Home »
C++ programs »
C++ Most popular & searched programs
C++ Program to print right angled pyramid of numbers
In this program, we are going to learn how to print numbered pyramids of numbers? This post contains solved program with output and explanation.
Submitted by Abhishek Pathak, on October 09, 2017 [Last updated : February 26, 2023]
Printing right angled pyramid of numbers
Today, we will share a program to print numbered pyramids of numbers. You must be familiar with these types of programs and should try to do it more often, because it will sharpen your skills.
Here, we are going to implement following pattern:
1
2 3
4 5 6
7 8 9 10
C++ code to print right angled pyramid of numbers
#include <iostream>
using namespace std;
int main() {
int i, rows, j, k = 1;
cout << "Enter the number of rows: ";
cin >> rows;
for (i = 1; i <= rows; i++) {
for (j = 1; j <= i; j++) {
cout << k << "\t";
k++;
}
cout << "\n";
}
return 0;
}
Output
Enter the number of rows: 5
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Explanation
We take the number of rows as the limit in the loop. The outer loop is for taking care of rows and the inner loop will take of the columns. Note that we have added the initializer j in the loop, as we need the value of k to increment for the next iterations as well. After the second loop we have printed a new line in order to show a new row.
This was the left oriented right angled puzzle. More puzzle like this coming soon.