C++ Descending Number Triangle Pattern (Right-Aligned)
Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview
Definition
What Is This Pattern?
A right-aligned descending number triangle uses a fixed-width inner loop: print spaces while j > i, then digits i..1 — so each row hugs the right edge.
Remember
Rule: for i from 1 to rows
for j from rows down to 1
if j > i: print space
else: print j
1
21
321
4321
54321 ← rows = 5
Follows the spaced mirror in Program 29; next is the number-star diamond in Program 31.
Approach
How to Solve It
Outer loop grows i. One fixed-width inner loop runs rows..1 — print a space or a digit so columns stay aligned.
Method
Idea
Best for
Fixed-width if/else
j > i → space; else print j
Learning, interviews, exams
Ternary form
(j > i) ? cout << " " : cout << j
Shorter demos once if/else is clear
Pseudocode
Pseudocode
for i from 1 to rows:
for j from rows down to 1:
if j > i: print space
else: print j
print newline
Cheat sheet
Goal
Pattern
Grow each row
for (i = 1; i <= rows; i++)
Fixed width
for (j = rows; j >= 1; j--)
Pad or digit
if (j > i) cout << " "; else cout << j;
Leading spaces
rows - i spaces before digits
End of row
cout << "\n";
Printing Numbers vs Starting a New Line
API
Effect
Use for
cout << j / " "
Stays on the same line
Each space or digit on the row
cout << "\n"
Ends the current line
After the fixed-width loop finishes
Print characters without a newline, then end the row once.
Try it
Live Preview
Change the row count and the right-aligned triangle updates instantly — capped at 9 for readable demos.
Whole numbers from 1 to 9. Tap a chip or type a value — the preview redraws as you go.
Live resultrows = 5 · 25 chars
1
21
321
4321
54321
Trace
Worked Walkthrough — rows = 5
Trace how leading spaces shrink and descending digits grow on each row.
i
Spaces
Digits
Prints
1
4
1
1
2
3
21
21
3
2
321
321
4
1
4321
4321
5
0
54321
54321
Leading spaces = rows − i. Each row prints rows characters → O(n²) total.
Code
C++ Programs
Three complete programs: fixed rows = 5, cin input with ternary form, and a compact rows = 3 demo. Use View Output to reveal sample results.
Example 1 — Fixed rows = 5
Hard-coded height — fixed-width descending loop with if/else for space vs digit.
C++
#include <iostream>
using namespace std;
int main()
{
int i, j;
for (i = 1; i <= 5; i++)
{
for (j = 5; j >= 1; j--)
{
if (j > i)
cout << " ";
else
cout << j;
}
cout << "\n";
}
return 0;
}
Output
1
21
321
4321
54321
How It Works
1. Outer grows the digit count.i is how many digits appear on the right side of the row.
2. Fixed width. Inner loop always runs 5 times — unused left slots become spaces.
3. Descending digits. When j <= i, print j so the filled part reads i..1.
Example 2 — User Input Rows
Read rows with cin, validate, then use a ternary for each position.
C++
#include <iostream>
using namespace std;
int main()
{
int rows;
int i, j;
cout << "Enter rows: ";
if (!(cin >> rows) || rows <= 0)
{
cout << "Please enter a positive integer.\n";
return 1;
}
for (i = 1; i <= rows; i++)
{
for (j = rows; j >= 1; j--)
(j > i) ? cout << " " : cout << j;
cout << "\n";
}
return 0;
}
Output (when user enters 4)
Enter rows: 4
1
21
321
4321
How It Works
1. Prompt and validate. Reject failed reads and non-positive values before printing.
2. Same core. Ternary form matches Example 1’s if/else — only rows comes from the user.
3. Safer input tip. Cap demos for readable output:
Safer input
if (!(cin >> rows) || rows < 1 || rows > 9)
{
cout << "Enter a whole number from 1 to 9.\n";
return 1;
}
Example 3 — Compact rows = 3
Same structure with only three rows — easy to confirm spaces shrink by one each time.
C++
#include <iostream>
using namespace std;
int main()
{
int rows = 3;
int i, j;
for (i = 1; i <= rows; i++)
{
for (j = rows; j >= 1; j--)
{
if (j > i)
cout << " ";
else
cout << j;
}
cout << "\n";
}
return 0;
}
Output
1
21
321
How It Works
1. Three rows.i = 1 → 1; i = 2 → 21; i = 3 → 321.
2. Trace on paper. If the inner loop stops at i instead of rows, the triangle loses right alignment.
3. Scale up next. Once the small demo is clear, use Examples 1–2 for five rows or user input.
Edge Cases & Pitfalls
Check these before calling the solution done.
no spaces
Missing else for spaces
Skipping cout << " " when j > i left-aligns the triangle — you get Program 3’s shape without padding.
width ≠ rows
Loop only to i
Inner loop must always run rows times. Stopping at i removes leading spaces and breaks alignment.
j++
Ascending inner loop
for (j = 1; j <= rows; j++) changes digit order and padding. Keep j = rows..1.
\n inside
Broken rows
If cout << "\n" sits inside the inner loop, you get one character per line. Call it only after the loop.
rows = 1
Single digit
Output is just 1 with no leading spaces. A good sanity check for input validation.
cin
Check the stream
Validate cin >> rows before looping — a failed read leaves rows unset or stale.
Analysis
Time and Space Complexity
Program
Time
Extra space
Fixed / input (Examples 1–2)
O(n²)
O(1)
Compact rows = 3 (Example 3)
O(n²)
O(1)
Each of n rows prints n characters → O(n²) time. Only a few loop variables are needed.
Remember
Key Takeaways
One fixed loop:j = rows..1 prints space when j > i, else digit j.
Always width rows: fixed width is what keeps the triangle right-aligned.
Break the row: spaces and digits in the inner loop; cout << "\n" after it finishes.
Complexity:O(n²) time from n² characters; O(1) extra space.
One line: for each row i, walk j = rows..1 and print space or j, then newline.
Frequently Asked Questions
A right-aligned descending triangle: for rows=5 you get 1 / 21 / 321 / 4321 / 54321 — leading spaces pad each row to width rows.
Leading spaces are printed while j > i. Smaller rows get more spaces, pushing digits to the right edge.
The inner loop runs j from rows down to 1. When j <= i, it prints j — naturally producing i..1 on each row.
Running j from rows down to 1 on every row keeps column alignment. Spaces fill positions where j > i.
Program 3 prints a left-aligned reverse descending triangle with no leading spaces. Program 30 pads with spaces for right alignment.
cout << j (or a space) stays on the same line. cout << "\n" ends the row after the fixed-width loop finishes.
After prompting, use if (!(cin >> rows) || rows <= 0) to reject bad input before printing.
O(n²) for n rows because each row runs a fixed-width inner loop of n iterations.
🤔
Did you know?
This pattern uses a fixed column width (rows). For each row i, the inner loop prints spaces while j > i, then prints digits in descending order — producing a right-aligned triangle.