Right-Aligned Descending Number Triangle in C++

What You’ll Learn
How to print a right-aligned descending number triangle in C++: each row ends with a descending sequence like 21, 321, 4321, etc.
The key idea is to print spaces first for positions that should be empty, then print numbers once the condition is met.
⭐ Pattern Output
For rows = 5, the pattern looks like this:
1
21
321
4321
54321Complete C++ Program
The inner loop runs from rows down to 1. For each row i, print a space when j > i, otherwise print j.
#include <iostream>
using namespace std;
int main() {
int rows = 5;
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;
}🧠 How It Works
Set the size
int rows = 5; defines how many lines to print.
Outer loop controls the row
for (i = 1; i <= rows; i++) increases how many digits appear on each line.
Inner loop scans from rows down to 1
for (j = rows; j >= 1; j--) decides what to print at each position.
Spaces then digits
If j > i, print a space. Otherwise, print j. That right-aligns the triangle while producing i..1.
Right-aligned triangle
There are rows iterations of the inner loop for each row, so time complexity is O(n²).
Variation — User Input Version
Let the user decide the number of rows using cin:
#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 >= 1; j--) {
if (j > i)
cout << " ";
else
cout << j;
}
cout << "\n";
}
return 0;
}💡 Tips for Enhancement
Try These
- Add spaces between digits using
cout << j << ' ' - Print the same pattern left-aligned by skipping the space branch
- Replace digits with characters to generate alphabet triangles
- Print an inverted version by iterating
ifromrowsdown to 1 - Use formatting (like
setw) for consistent alignment in wider patterns
Avoid
- Forgetting the newline after each row
- Hard-coding the value 5 in multiple places instead of using
rows - Mixing tabs and spaces for alignment (tabs can render differently)
- Using
endlin loops unnecessarily (it flushes output)
Key Takeaways
Print leading spaces when j > i to right-align the triangle.
Print the descending digits i, i-1, ..., 1 when j <= i.
The inner loop always runs from rows down to 1, so each row has a fixed width.
This pattern is a great intro to alignment logic used in many console patterns.
❓ Frequently Asked Questions
rows down to 1, printing either a space or a digit each time. That guarantees a fixed number of characters per row.j from i down to 1 (and don’t print leading spaces).j <= i (for example print rows - j + 1) to shift the digit values.Explore More C++ Number Patterns!
Next, try combining right-alignment with palindromes or stars to create richer shapes.
Right-alignment in console patterns usually comes down to printing leading spaces. Once you master that, many pyramid and diamond patterns become much easier.
12 people found this page helpful
