Ascending Number Pattern with Right Padding in C

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Two Inner Loops

What You’ll Learn

How to print an ascending number pattern with right padding in C. Each row begins with an ascending sequence, then fills the rest of the row with the maximum value (the row count).

This is a great exercise for splitting row output into two parts: sequence + padding.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

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

Complete C Program

The first inner loop prints i..rows. The second inner loop prints rows repeatedly to fill the right side.

c
#include <stdio.h>

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

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

    return 0;
}

🧠 How It Works

1

Set the maximum value

rows = 5 defines both the triangle size and the right padding number.

Setup
2

Outer loop counts down

for (i = rows; i >= 1; --i) decides the first number of each row.

Row control
3

Print the ascending sequence

for (j = i; j <= rows; ++j) prints i i+1 ... rows.

Sequence
4

Pad the right side with rows

for (k = 1; k < i; ++k) prints rows repeatedly so each row has exactly rows numbers.

Padding
=

Sequence + padding per row

Each row prints exactly rows numbers. Total operations \(\approx rows \times rows\), so time complexity is O(n²).

2

Variation — User Input Version

Let the user choose the number of rows at runtime:

c
#include <stdio.h>

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

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

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

    return 0;
}

💡 Tips for Enhancement

Try These

  • Remove the padding loop to print only the ascending sequence per row
  • Change the padding value (use i or 0) to create new patterns
  • Right-align by printing leading spaces before the sequence
  • Print without trailing spaces by formatting the last number differently
  • Replace numbers with characters to create padded alphabet patterns

Avoid

  • Mixing up loop bounds (keep the sequence as j = i..rows)
  • Forgetting the newline after each row
  • Using uninitialized variables when moving code around
  • Assuming fixed rows when you want a scalable program
  • Letting invalid input (like negative rows) pass unchecked

Key Takeaways

1

Each row prints an ascending sequence from i up to rows.

2

The remaining positions are filled using right padding (printing rows).

3

Two inner loops keep the row logic clear: one for the sequence, one for the padding.

4

Total work is proportional to for n rows.

❓ Frequently Asked Questions

The first row starts at i = rows, so the ascending sequence is just 5. The rest of the row is filled by the padding loop, which also prints 5.
Without padding, each row would have a different length. Padding ensures every row prints exactly rows numbers, giving the right-filled block effect.
Remove the second inner loop. Then each row will print only i..rows, such as 5, 4 5, 3 4 5 and so on.
O(n²) for n rows because every row prints n numbers in total.

Explore More C Number Patterns!

Learn spacing, alignment, and padding techniques by practicing more number patterns.

All Number Patterns →
Did you know?

Many grid-like patterns are just two parts per row: print what changes first, then fill the rest with a constant (padding). This page is a simple example of that idea.

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