Right-Aligned Descending Number Triangle in C

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

What You’ll Learn

How to print a right-aligned descending number triangle in C. Each row prints values from the row number down to 1.

Leading spaces are printed before each row so the triangle aligns on the right.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
    1
   21
  321
 4321
54321
1

Complete C Program

First print rows - i spaces, then print i..1 in descending order.

c
#include <stdio.h>

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

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

    return 0;
}

🧠 How It Works

1

Choose the number of rows

int rows = 5; sets how many lines to print.

Setup
2

Print leading spaces

rows - i spaces push numbers to the right so each row aligns to the right edge.

Alignment
3

Print descending numbers

for (k = i; k >= 1; k--) prints i..1 each row.

Numbers
=

Right-aligned triangle

Spaces decrease as rows increase, producing a right-aligned triangular shape.

2

Variation — User Input Version

Read rows from the user and print the same right-aligned pattern.

c
#include <stdio.h>

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

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

    return 0;
}

💡 Tips for Enhancement

Try These

  • Print ascending numbers instead of descending for a new look
  • Remove spaces to make it left-aligned
  • Add spaces between numbers with printf("%d ", k)
  • Use characters instead of numbers to create alphabet patterns
  • Convert it into a centered pyramid by adjusting spaces

Avoid

  • Forgetting the newline after each row
  • Using tabs for spacing (alignment varies)
  • Not validating user input for negative rows
  • Mixing up which loop prints spaces vs numbers

Key Takeaways

1

The space loop controls right alignment (rows - i).

2

The number loop prints i..1 to create descending rows.

3

Right alignment is just a spacing trick added before the numbers.

4

Nested loops are the foundation for most pattern problems.

❓ Frequently Asked Questions

Each row prints descending digits (i..1) and starts with rows - i spaces, so all rows align to the right.
Spaces shift the numbers to the right. As the row length grows, we print fewer spaces to keep the right edge aligned.
Yes. Remove the loop that prints spaces.
O(n²) for n rows due to nested loops.

Explore More C Number Patterns!

Right alignment and spacing logic show up in many advanced pattern problems.

All Number Patterns →
Did you know?

Right-aligned patterns are usually just left-aligned patterns plus a simple space loop. Once you master spacing, you can create centered and diamond patterns too.

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