🔹 1. Problem Description
The Number Pyramid is a common C++ pattern problem
where numbers are arranged in a pyramid-like structure. Unlike star patterns,
this one focuses on number progression and formatting. It tests your
understanding of:
✅ Objective:
Given an input n, print a pyramid of n rows using increasing
numbers row-wise.
Example (n = 5):
1
1 2
1 2 3
1 2 3 4
1
2 3 4 5
Each row starts from 1 and ends at the row number.
🔹 2. Flowchart
Start
|
|--
Input n
|
|--
For i = 1 to n:
| Print (n - i) spaces
| For j = 1 to i:
| Print j
| Print newline
|
End
✅ Focus on two main tasks:
🔹 3. Algorithm
🔽 Step-by-Step:
✅ Pseudocode:
Input:
n
for
i = 1 to n:
print (n - i) spaces
for j = 1 to i:
print j
print newline
🔹 4. C++ Code — Number
Pyramid
#include
<iostream>
using
namespace std;
int
main() {
int n;
cout << "Enter number of rows:
";
cin >> n;
for (int i = 1; i <= n; i++) {
// Print leading spaces
for (int space = 1; space <= n - i;
space++)
cout << " ";
// Print numbers
for (int j = 1; j <= i; j++)
cout << j << "
";
cout << endl;
}
return 0;
}
🔹 5. Output for n = 5
1
1 2
1 2 3
1 2 3 4
1
2 3 4 5
✅ Numbers increase per row,
aligned in a centered pyramid.
🔹 6. Output Breakdown
Table
|
Row |
Spaces |
Numbers
Printed |
|
1 |
4 |
1 |
|
2 |
3 |
1 2 |
|
3 |
2 |
1 2 3 |
|
4 |
1 |
1 2 3 4 |
|
5 |
0 |
1 2 3 4 5 |
🔹 7. Alternate Variations
🔁 Reverse Numbers:
1
2 1
3 2 1
4 3 2 1
5
4 3 2 1
Replace cout << j with cout << i - j + 1.
🔁 Continuous Number
Pyramid:
1
2 3
4 5 6
7 8 9 10
11
12 13 14 15
Add a counter:
int
num = 1;
for
(int i = 1; i <= n; i++) {
for (int space = 1; space <= n - i;
space++)
cout << " ";
for (int j = 1; j <= i; j++)
cout << num++ << "
";
cout << endl;
}
🔹 8. Complexity &
Considerations
|
Factor |
Value |
|
Time
Complexity |
O(n²) |
|
Space
Complexity |
O(1) |
|
Loops Used |
3 (nested) |
|
Output Lines |
n |
|
Alignment
Type |
Centered |
✅ This pattern builds logic
around triangular formatting and number progression.
🔹 9. Common Pitfalls
|
Mistake |
Solution |
|
Skipping
spaces |
Use separate
loop for n - i spaces |
|
Forgetting
endl |
Required to
start a new row |
|
Misusing
counter in variation |
Always reset
or increment carefully |
|
Off-by-one
loop errors |
Loop from 1
to i not 0 to i-1 |
They test your mastery over loops, logic building, and output formatting under pressure.
Understand for loops, nested loops, conditionals (if-else), and basic I/O.
✅ Yes. Most are based on nested loops (outer for rows, inner for columns/spaces/numbers).
Pay attention to the number of spaces before stars or digits. Use setw() or loops for space printing.
It’s a right-angled triangular pattern using consecutive numbers from 1 onward in row-wise fashion.
Use cin >> n and build loops that use n to determine number of rows/columns.
✅ They’re mostly asked at entry-level or internship roles, but understanding them helps in algorithmic thinking.
Use print statements for row and column counters; test smaller values first (n=3, n=5).
Tutorials are for educational purposes only, with no guarantees of comprehensiveness or error-free content; TuteeHUB disclaims liability for outcomes from reliance on the materials, recommending verification with official sources for critical applications.
Kindly log in to use this feature. We’ll take you to the login page automatically.
LoginReady to take your education and career to the next level? Register today and join our growing community of learners and professionals.
Your experience on this site will be improved by allowing cookies. Read Cookie Policy
Your experience on this site will be improved by allowing cookies. Read Cookie Policy
Comments(0)