Inverted Repeating Number Triangle in C

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

What You’ll Learn

How to print an inverted repeating number triangle in C. Each row repeats the same digit, starting from the maximum and decreasing down to 1, while the number of repeats also shrinks.

This is the “inverted” version of the repeating triangle (Program 9): instead of growing rows, rows become shorter each line.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
55555
4444
333
22
1
1

Complete C Program

The outer loop chooses the digit (i) and the inner loop prints it i times.

c
#include <stdio.h>

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

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

    return 0;
}

🧠 How It Works

1

Set rows

rows controls the height and the starting digit.

Setup
2

Outer loop (digit + length)

i goes from rows down to 1. This sets both the printed digit and the number of repeats on that row.

Shrink rows
3

Inner loop (repeat i)

j runs from 1 to i, printing i exactly i times.

Repeat count
4

New line

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

Line break
=

Inverted repeating triangle

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

    return 0;
}

💡 Tips for Enhancement

Try These

  • Add spaces with printf("%d ", i)
  • Flip the outer loop direction to get the ascending repeating triangle (Program 9)
  • Use letters for AAAAA, BBBB, CCC... style patterns
  • Right-align by printing leading spaces before each row

Avoid

  • Hard-coding the row count in your loops
  • Forgetting the newline after each row
  • Not validating user input when using scanf()

Key Takeaways

1

Row i repeats digit i exactly i times.

2

The triangle is inverted because the outer loop counts down, shrinking row lengths.

3

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

4

This is the inverted version of the repeating triangle (Program 9).

❓ Frequently Asked Questions

Because the outer loop starts at i = rows, and the inner loop runs i times, so the first row prints the maximum number of digits.
Change the outer loop to increment from 1 to rows and keep printing i inside the inner loop.
O(n²) because total prints are 1+2+…+n = n(n+1)/2.

Explore More Pattern Variations

Try combining repetition with alignment and spacing to create inverted pyramids and mirrored triangles.

All Number Patterns →
Did you know?

This is the “true inverted” version of the repeating triangle: instead of controlling repetition with a decreasing inner-loop range, you directly run the inner loop from 1 to i while i itself counts 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