C++ Program to Print Consecutive Column Numbers in Right Triangle

Write a C++ program to print consecutive column numbers in the right triangle pattern using for loop.

#include<iostream>
using namespace std;

int main()
{
	int i, j, k, rows;
     
    cout << "Enter Right Triangle of Consecutive Col Nums = ";
    cin >> rows;

    cout << "Consecutive Column Numbers in a Right Triangle Pattern\n"; 
    k = 1;
    for(i = 1; i <= rows; i++)
    {
    	for(j = 1; j <= i; j++)
		{
            cout << k++ << " ";
        }
        cout << "\n";
    }		
 	return 0;
}
C++ Program to Print Consecutive Column Numbers in Right Triangle

This C++ example prints the right angled triangle pattern of consecutive column numbers using a while loop.

#include<iostream>
using namespace std;

int main()
{
	int i, j, k, rows;
     
    cout << "Enter Right Triangle of Consecutive Col Nums = ";
    cin >> rows;

    cout << "Consecutive Column Numbers in a Right Triangle Pattern\n"; 
    k = 1;
    i = 1;
    while(i <= rows)
    {
        j = 1;
    	while( j <= i)
		{
            cout << k++ << " ";
            j++;
        }
        cout << "\n";
        i++;
    }		
 	return 0;
}
Enter Right Triangle of Consecutive Col Nums = 10
Consecutive Column Numbers in a Right Triangle Pattern
1 
2 3 
4 5 6 
7 8 9 10 
11 12 13 14 15 
16 17 18 19 20 21 
22 23 24 25 26 27 28 
29 30 31 32 33 34 35 36 
37 38 39 40 41 42 43 44 45 
46 47 48 49 50 51 52 53 54 55 

About Suresh

Suresh is the founder of TutorialGateway and a freelance software developer. He specialized in Designing and Developing Windows and Web applications. The experience he gained in Programming and BI integration, and reporting tools translates into this blog. You can find him on Facebook or Twitter.