Right-Aligned Descending Number Triangle in C

Beginner
⏱️ 5 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Nested Loops

What You’ll Learn

How to print a right-aligned descending number triangle in C using nested loops. Each row starts at the maximum number (rows) and counts down, but the row length decreases each line.

The key is to keep the outer loop increasing from 1 to rows, while the inner loop counts down from rows to i.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
54321
5432
543
54
5
1

Complete C Program

The inner loop prints from rows down to i, so each next row has one fewer number.

c
#include <stdio.h>

int main() {
    int rows = 5;
    int i, j;

    for (i = 1; i <= rows; ++i) {
        for (j = rows; j >= i; --j) {
            printf("%d", j);
        }
        printf("\n");
    }

    return 0;
}

🧠 How It Works

1

Setup

rows defines the height and the largest printed number.

Setup
2

Outer loop (rows)

i goes from 1 to rows. As i increases, the row length shrinks.

Row length
3

Inner loop (descending print)

j starts at rows and decrements until i, printing rows..i on each row.

Countdown
4

New line

printf("\n") moves to the next row.

Line break
=

Right-aligned descending triangle

Total prints are n(n+1)/2 so time complexity is O(n²) for n rows.

2

Variation — User Input Version

Accept the number of rows from the user using scanf():

c
#include <stdio.h>

int main() {
    int rows;
    int i, j;

    printf("Enter the number of rows: ");
    scanf("%d", &rows);

    for (i = 1; i <= rows; ++i) {
        for (j = rows; j >= i; --j) {
            printf("%d", j);
        }
        printf("\n");
    }

    return 0;
}

💡 Tips for Enhancement

Try These

  • Add spaces between numbers with printf("%d ", j)
  • Convert to an ascending triangle by flipping the inner loop direction
  • Right-align visually with leading spaces (if you add spaces between digits)
  • Try different rows values to see the shape scale

Avoid

  • Starting the inner loop at 1 (it won’t start from the maximum)
  • Forgetting printf("\n") after each row
  • Not validating input when accepting user rows

Key Takeaways

1

Each row starts at rows and counts down, because the inner loop begins at j = rows.

2

Row length shrinks because the inner loop stops at j = i.

3

Total prints are n(n+1)/2, so time complexity is O(n²).

4

This pattern is closely related to Program 2, but prints numbers in reverse order.

❓ Frequently Asked Questions

Because the inner loop always begins with j = rows, so the first printed number is always the maximum.
The loop stops at j = i. Since i increases each row, fewer values satisfy j >= i, so the row gets shorter.
Change the inner loop to for (j = i; j <= rows; ++j) and print j.
O(n²) because total prints are n+(n-1)+…+1 = n(n+1)/2.

Practice More Number Patterns

Changing just the loop start and end values can create dozens of patterns—keep experimenting.

All Number Patterns →
Did you know?

This pattern is a mirror of Program 2’s idea: Program 2 prints 1..rows starting from the row index, while here you print rows..i by counting down.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful