Number Triangle program in C++


Today I am going to show you the code how to print different type of number Triangle program in C++/C. For all these program , we used dev C++ which is freely available on the net.

We are starting our journey with the simplest triangle, as soon as you will grab the code of this code a new problem based on the same method will be given to you.

1

1    2

1     2       3

1      2      3      4

1      2       3      4       5

The code to generate this Triangle is as follows

#include<iostream>
#include<iomanip>
#include<conio.h>
using namespace std;
int main()
{
int i,j;
for(i=1;i<=10;i++)
{
for(j=1;j<=i;j++)
cout<<setw(4)<<j;
cout<<endl;
}
cout<<"\n\n\n\n\n Press any key to continue...";
getch();
return 0;
}

2nd Digital Triangle

The next Triangle we are going to generate is this

1
2 2
3 3 3
4 4 4 4
5 5 5 5 5

The code to generate this Type of Triangle is same, You have to just change the cout<< value from J to i

#include<iostream>
#include<iomanip>
#include<conio.h>
using namespace std;
int main()
{
    int i,j;
    for(i=1;i<=10;i++)
     {
       for(j=1;j<=i;j++)
           cout<<setw(4)<<i;
           cout<<endl;
     }
  cout<<"\n\n\n\n\n Press any key to continue...";
  getch();
  return 0;
}  

3rd Digital Triangle

Now you know how to print digital Triangle using for loop, now its time to extend our concept. What if we want to print the following pattern which print our number from right to left.

1
2 2
3 3 3
4 4 4 4
5 5 5 5 5

NOTE: Since our system always prints values from right to left, to overcome this issue , first of all we will print first blank spaces and then our number.

#include<iostream>
#include<iomanip>
#include<conio.h>
using namespace std;
int main()
{
    int i,j,k;
    for(i=1;i<=10;i++)
     {
       for (k=1;k<=(10-i);k++) //k is used to print space
            cout<<setw(4)<<" ";                  
       for(j=1;j<=i;j++)
           cout<<setw(4)<<i;
           cout<<endl;
     }
  cout<<"\n\n\n\n\n Press any key to continue...";
  getch();
  return 0;
}   

 

4th digital Triangle

[pre]

1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1

[/pre]

Code to generate this pattern is the combination of the 3rd and 1st digital Triangle.


#include<iostream>
#include<iomanip>
#include<conio.h>
using namespace std;
int main()
{
int i,j,k,L;
for(i=1;i<=10;i++)
{
for (k=1;k<=(10-i);k++) //k is used to print space
cout<<setw(4)<<" ";
for(j=1;j<=i;j++)
cout<<setw(4)<<j;
for(L=i-1;L>=1;L--)
cout<<setw(4)<<L;
cout<<endl;
}
cout<<"\n\n\n\n\n Press any key to continue...";
getch();
return 0;
}

5th  Digital Pattern – Digital Rhomus

1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
1 2 3 4 3 2 1
1 2 3 2 1
1 2 1
1

Now combine 4th Method 2 times


#include<iostream>
#include<iomanip>
#include<conio.h>
using namespace std;
int main()
{
int i,j,k,L;
for(i=1;i<=10;i++)
{
for (k=1;k<=(10-i);k++) //k is used to print space
cout<<setw(4)<<" ";
for(j=1;j<=i;j++)
cout<<setw(4)<<j;
for(L=i-1l;L>=1;L--)
cout<<setw(4)<<L;
cout<<endl;
}

for(i=9;i>=1;i--)
{
for (k=1;k<=(10-i);k++) //k is used to print space
cout<<setw(4)<<" ";
for(j=1;j<=i;j++)
cout<<setw(4)<<j;
for(L=i-1l;L>=1;L--)
cout<<setw(4)<<L;
cout<<endl;
}
cout<<"\n\n\n\n\n Press any key to continue...";
getch();
return 0;
}

These are some of very basic Triangles which we can print using for loop, If you have any other issue that you are right now facing to print any such digital / numerical pattern, feel free to send your queries as a comment.




Suggested Similar Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>