Ascending Repeating Shrinking Pattern in C

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

What You’ll Learn

How to print an ascending repeating number shrinking pattern in C. The digit increases each row, but the number of repeats decreases each row.

This is a great exercise to learn how expressions like rows - i + 1 can control the inner-loop length.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
11111
2222
333
44
5
1

Complete C Program

The inner loop runs rows - i + 1 times, which shrinks the row as i increases.

c
#include <stdio.h>

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

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

    return 0;
}

🧠 How It Works

1

Set rows

rows is the maximum digit and also controls triangle height.

Setup
2

Outer loop (digit)

i increases from 1 to rows, choosing the digit to repeat on each row.

Digit increases
3

Inner loop (repeat count)

rows - i + 1 gives the repetition count. It decreases by one each row, shrinking the line length.

Rows shrink
4

New line

printf("\n") completes the row.

Line break
=

Shrinking repetition

Total prints are 1+2+…+n = n(n+1)/2, so runtime is O(n²).

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 = 1; j <= rows - i + 1; ++j) {
            printf("%d", i);
        }
        printf("\n");
    }

    return 0;
}

💡 Tips for Enhancement

Try These

  • Make it grow by using j <= i instead of rows - i + 1
  • Add spaces with printf("%d ", i)
  • Try letters instead of digits for AAAA, BBB, CC...
  • Right-align by adding leading spaces before each row

Avoid

  • Hard-coding the row count
  • Forgetting the newline after each row
  • Using negative rows without validation

Key Takeaways

1

The printed digit is the row index i.

2

The repetition count is rows - i + 1, which decreases each row.

3

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

4

This is the shrinking counterpart to repeating triangles like Program 9.

❓ Frequently Asked Questions

Because the inner loop runs only rows - i + 1 times, which decreases as i increases.
Use for (j = 1; j <= i; ++j) so each next row prints one more digit.
O(n²) because total prints are 1+2+…+n = n(n+1)/2.

Explore More Pattern Variations

Try combining repetition rules with alternating row logic for new zigzag patterns.

All Number Patterns →
Did you know?

The expression rows - i + 1 is a common trick for shrinking patterns. You’ll see it again in inverted triangles and right-aligned shapes.

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