Right-Aligned Reverse Number Triangle in C

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

What You’ll Learn

How to print a right-aligned reverse number triangle in C. Each row prints numbers from a maximum value (like 5) down to a row-specific minimum, and leading spaces align the triangle to the right.

This is a good practice problem for understanding nested loops, spacing logic, and descending sequences.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
        5
      5 4
    5 4 3
  5 4 3 2
5 4 3 2 1
1

Complete C Program

We print rows - i groups of spaces, then print values from rows down to rows - i + 1.

c
#include <stdio.h>

int main() {
    int rows = 5;

    for (int i = 1; i <= rows; i++) {
        for (int j = 1; j <= rows - i; j++) {
            printf("  ");
        }

        for (int k = rows; k >= (rows - i + 1); k--) {
            printf("%d ", k);
        }

        printf("\n");
    }

    return 0;
}

🧠 How It Works

1

Pick the maximum value

With rows = 5, the maximum printed value is also 5.

Setup
2

Outer loop decides the row length

Row i prints exactly i numbers.

Row control
3

Spaces create right alignment

The loop printing " " runs rows - i times, pushing numbers to the right.

Alignment
4

Descending sequence per row

We print k from rows down to rows - i + 1, which yields 5; then 5 4; then 5 4 3; and so on.

Number printing
=

Right-aligned reverse triangle

Total printed values are 1+2+…+n = n(n+1)/2, so runtime is O(n²) for n rows.

2

Variation — User Input Version

Accept the row count using scanf():

c
#include <stdio.h>

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

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

    return 0;
}

💡 Tips for Enhancement

Try These

  • Remove the space loop to make it left-aligned
  • Print ascending values by looping from rows - i + 1 up to rows
  • Use a different maximum value by changing rows
  • Turn it into a centered pyramid by adjusting spaces
  • Swap numbers for letters to create alphabet patterns

Avoid

  • Forgetting printf("\n") after each row
  • Printing the wrong number of spaces (breaks alignment)
  • Using an incorrect stop condition for the descending loop
  • Assuming rows can be negative (validate user input)

Key Takeaways

1

Leading spaces make the triangle right-aligned.

2

Each row prints a descending sequence from rows.

3

The minimum per row is rows - i + 1.

4

Runtime is O(n²) for n rows.

❓ Frequently Asked Questions

Because the inner loop starts from k = rows on every row, so the first printed value is always the maximum.
Remove the loop that prints printf(" ") before the numbers.
It decreases the stop value by 1 each row, so each new row prints one extra number.
O(n²) for n rows: total prints are 1+2+…+n = n(n+1)/2.

Explore More C Number Patterns!

Right-aligned patterns are great practice for spacing logic—keep going with the next program.

All Number Patterns →
Did you know?

Many right-aligned patterns rely on the same idea: print spaces first, then print the row content. Once you master spacing, pyramids and diamonds become much easier.

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