C++ Descending Number Triangle Pattern (Left-Aligned)
Beginner
6 min read
Updated: Sep 2026
3 programs
Live preview
Definition
What Is This Pattern?
A left-aligned descending number triangle always starts each row at the maximum digit (rows) and counts down, while the stopping digit rises so the reverse tail gets shorter.
Remember
Rule: for stop i from 1 to rows,
print rows down through i
54321
5432
543
54
5 ← 5 rows (left edge fixed at 5)
Unlike Program 3 (left edge moves: 4321, 321, …), here every row begins at the same top digit. Same shrinking widths; fixed left edge.
Approach
How to Solve It
Raise the stop value each row; always count down from rows to that stop.
Method
Idea
Best for
Nested loops
Outer raises stop i; inner prints rows..i
Learning, interviews, exams
Spaced digits
Same loops; print j then a space
Readable demos once the shape clicks
Pseudocode
Pseudocode
for i from 1 to rows:
for j from rows down to i:
print j (no newline)
print newline
Print digits without a newline, then end the row once. cout << endl also ends the line and flushes; "\n" is enough for these demos.
Try it
Live Preview
Change the row count and the descending triangle updates instantly — including the triangular digit total.
Whole numbers from 1 to 9 (single digits). Tap a chip or type a value — the preview redraws as you go.
Live result5 rows · 15 digits
54321
5432
543
54
5
Trace
Worked Walkthrough — rows = 4
Trace each outer-loop stop value as i rises from 1 to 4 with the top digit fixed at 4.
Stop i
Inner j
Printed row
Digits
1
4..1
4321
4
2
4..2
432
3
3
4..3
43
2
4
4..4
4
1
Total digit prints: 4 + 3 + 2 + 1 = 10 = 4×5/2. Every row starts at 4 — only the tail shortens.
Code
C++ Programs
Three complete programs: fixed rows, cin input, and spaced digits. Use View Output to reveal sample results.
Example 1 — Fixed rows = 5
Hard-coded row count — every row starts at 5; the stop digit rises to shorten the tail.
C++
#include <iostream>
using namespace std;
int main()
{
int rows = 5;
int i, j;
for (i = 1; i <= rows; i++)
{
for (j = rows; j >= i; j--)
cout << j;
cout << "\n";
}
return 0;
}
Output
54321
5432
543
54
5
How It Works
1. Outer loop raises the stop.i runs from 1 to rows — longest row first.
2. Inner loop always starts at rows. For each stop, j runs from rows down to i, so the row is rows..i.
3. Print digits, then break the line.cout << j stays on the row; cout << "\n" after the inner loop starts the next (shorter) row.
When i = 1 you get 54321; when i = 5 you get 5.
Example 2 — User Input Version
Read the row count at runtime. Prefer validating cin (shown in the tip below).
C++
#include <iostream>
using namespace std;
int main()
{
int rows;
int i, j;
cout << "Enter the number of rows: ";
cin >> rows;
for (i = 1; i <= rows; i++)
{
for (j = rows; j >= i; j--)
cout << j;
cout << "\n";
}
return 0;
}
Output (when user enters 4)
Enter the number of rows: 4
4321
432
43
4
How It Works
1. Prompt and read. Ask for a row count, then store it in rows.
2. Same fixed-start core. Only the source of rows changes — the print logic matches Example 1.
3. Safer input tip. Prefer:
Safer input
if (!(cin >> rows) || rows < 1)
{
cout << "Enter a whole number of rows (1 or more).\n";
return 1;
}
Example 3 — Spaced Digits
Keep the same loop bounds; print each digit followed by a space for easier reading.
C++
#include <iostream>
using namespace std;
int main()
{
int rows = 5;
int i, j;
for (i = 1; i <= rows; i++)
{
for (j = rows; j >= i; j--)
cout << j << " ";
cout << "\n";
}
return 0;
}
Output
5 4 3 2 1
5 4 3 2
5 4 3
5 4
5
How It Works
1. Same bounds. Outer and inner loops match Example 1 exactly.
2. Only the print changes.cout << j << " " inserts a space after each digit.
3. Trailing space. Each row ends with a space before the newline — fine for demos; trim later if you need exact width.
Edge Cases & Pitfalls
Check these before calling the solution done.
j = i..1
Program 3 by mistake
If the outer loop descends and the inner prints i..1, you get Program 3. Keep j = rows; j >= i; j-- with an ascending outer loop.
j >= 1
No shrinking
Stopping at 1 every time reprints the full reverse run. The stop must be the rising outer variable i.
j = 1..i
Ascending triangle
An ascending inner bound prints Program 1’s shape. Count down from rows to i.
\n early
Column of digits
If cout << "\n" is inside the inner loop, each digit lands on its own line. End the row only after the inner loop.
rows = 1
Single digit
Output is just 1. A good sanity check.
Bad cin
Validate rows
Check cin >> rows and require rows >= 1 before the outer loop.
Analysis
Time and Space Complexity
Program
Time
Extra space
Nested loops (Examples 1–3)
O(rows²)
O(1)
Total digits = n + (n - 1) + … + 1 = n(n + 1)/2 — quadratic in n. Only a few integers of extra memory.
Remember
Key Takeaways
Rule: always start at rows; raise stop i so each reverse row shortens.
vs Program 3: same widths — here the left edge stays fixed; there the left edge moves.
Break the row: call cout << "\n" only after the inner loop.
Complexity:O(n²) time from the triangular digit count; O(1) extra space.
One line: for stop i from 1 to rows, print rows down through i, then end the line.
Frequently Asked Questions
Because the inner loop always begins at rows (the maximum digit) and counts down. Only the stopping point i changes per row.
Row i prints rows - i + 1 digits — the inner loop runs from j = rows down to i.
Program 3 prints i..1 with a descending outer loop (4321, 321, …). Program 4 prints rows..i with an ascending outer loop — every row starts at rows.
No — digits are left-aligned with no leading spaces. Each row begins flush left at the maximum digit.
Use cout << j << " " instead of cout << j — see Example 3.
Printing a digit stays on the same line. Printing a newline ends the current line. Digits use cout without a newline; the row break uses cout << "\n" after the inner loop.
O(n²) for n rows because total prints are 1 + 2 + ... + n = n(n+1)/2.
After cin >> rows, check failure and require rows ≥ 1 before the outer loop.
🤔
Did you know?
Each row starts at rows and counts down to i. Row i prints rows - i + 1 digits — total prints = n(n+1)/2; output is left-aligned with no leading spaces.