Inverted Repeating Number Triangle in C

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

What You’ll Learn

How to print an inverted repeating number triangle in C. Each row repeats one digit several times, and the number of repeats decreases on each next line.

The printed digit increases up to the middle row and then decreases (a mirror pattern).

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
11111
2222
333
22
1
1

Complete C Program

The inner loop repeats values fewer times as i grows. A small condition decides which number to print.

c
#include <stdio.h>

int main() {
    int i, j;

    for (i = 1; i <= 5; i++) {
        for (j = i; j <= 5; j++) {
            if (i < 4) {
                printf("%d", i);
            } else {
                printf("%d", 6 - i);
            }
        }
        printf("\n");
    }

    return 0;
}

🧠 How It Works

1

Outer loop controls rows

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

Row control
2

Inner loop decides repeat count

for (j = i; j <= 5; j++) runs 5, 4, 3, 2, 1 times respectively.

Repetition
3

Condition creates the mirror effect

Rows 1–3 print i (1, 2, 3). Rows 4–5 print 6 - i (2, 1).

Mirror
=

Inverted repeating number triangle

Repeat count decreases each row; values mirror around the center row.

2

Variation — User Input Version

This version reads the row count and uses a clean mirror rule that works for any positive rows.

c
#include <stdio.h>

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

    int i, j;
    int mid = (rows + 1) / 2;

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

    return 0;
}

💡 Tips for Enhancement

Try These

  • Add spaces between digits: printf("%d ", val)
  • Validate that rows is positive before printing
  • Right-align the pattern by printing leading spaces
  • Replace digits with letters for alphabet variations
  • Print a full diamond by adding a second half with increasing width

Avoid

  • Hardcoding the midpoint when you use user input
  • Forgetting the newline after each row
  • Mixing row and repeat logic (outer loop = rows, inner loop = repeats)
  • Using a mirror rule that breaks for even row counts

Key Takeaways

1

The inner loop runs rows - i + 1 times, so each row gets shorter.

2

A mirror value creates the sequence 1, 2, 3, 2, 1.

3

Time complexity is O(n²) because total prints are n(n+1)/2.

4

Small conditions like this show up in many pyramid and diamond patterns.

❓ Frequently Asked Questions

Rows get shorter from top to bottom (that’s the inverted shape), and each row repeats a single digit multiple times. The digit values mirror around the center: 1, 2, 3, 2, 1.
It decides whether we are in the increasing half (print i) or the decreasing half (print a mirrored value like rows + 1 - i).
Yes. Read rows with scanf(), compute mid = (rows + 1) / 2, and mirror with rows + 1 - i for rows after the midpoint.
O(n²) for n rows. Total printed digits are n(n+1)/2.

Explore More C Number Patterns!

Practice more triangles and pyramids to master row/column thinking.

All Number Patterns →
Did you know?

The repeat counts (5, 4, 3, 2, 1) follow a simple formula: row i prints rows - i + 1 times. That’s why the total printed digits is rows(rows+1)/2.

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