c++ program to print triangle pattern

Code:

#include <iostream>

using namespace std;

int main()
{
    int n;
    cin>>n;
    int i,j;
    int k=1;
    for(i=n-1;i>=0;i--)
    {
        int num=k;
        for(j=0;j<=i;j++)
        {
            cout<<num<<" ";
            num=num+n-j;
        }
        k++;
        cout<<"\n";
    }
}

Output:

1 8 14 19 23 26 28                                                                                                                             
2 9 15 20 24 27                                                                                                                                
3 10 16 21 25                                                                                                                                  
4 11 17 22                                                                                                                                     
5 12 18                                                                                                                                        
6 13                                                                                                                                           

6                                                                                                                                              
1 7 12 16 19 21                                                                                                                                
2 8 13 17 20                                                                                                                                   
3 9 14 18                                                                                                                                      
4 10 15                                                                                                                                        
5 11                                                                                                                                           
6   

Comments

Popular posts from this blog

c++ program to print string along the diagonals of the matrix.

Kollywood Game

C program to print the following Pattern