Repeating Number Triangle in C

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

What You’ll Learn

How to print a repeating number triangle in C. Each row repeats the row number: row 1 prints one 1, row 2 prints two 2s, and so on.

The important trick is printing i (row index) inside the inner loop, instead of printing j.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
1
22
333
4444
55555
1

Complete C Program

The inner loop controls how many times to print, while printf("%d", i) prints the repeating digit for that row.

c
#include <stdio.h>

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

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

    return 0;
}

🧠 How It Works

1

Setup

rows = 5 sets how many lines to print.

Setup
2

Outer loop (row number)

i goes from 1 to rows. This value decides which digit will repeat on the row.

Pick the digit
3

Inner loop (repeat count)

j runs from 1 to i, so the digit is printed exactly i times.

Repeat i times
4

New line

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

Line break
=

Repeating triangle

Total prints are 1+2+…+n = n(n+1)/2, so time complexity 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 <= i; ++j) {
            printf("%d", i);
        }
        printf("\n");
    }

    return 0;
}

💡 Tips for Enhancement

Try These

  • Print j instead of i for an ascending triangle
  • Add spaces with printf("%d ", i)
  • Use characters instead of digits for repeating alphabet triangles
  • Try a hollow version by printing only borders

Avoid

  • Printing j if your goal is repetition (it changes the pattern)
  • Forgetting the newline after each row
  • Not validating user input

Key Takeaways

1

Row i prints the same digit because you print i inside the inner loop.

2

The inner loop runs i times, creating the triangle growth.

3

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

4

Switching from printing i to j gives a completely different triangle.

❓ Frequently Asked Questions

Because row 2 runs the inner loop twice and prints i (which is 2) each time.
Print j instead of i: use printf("%d", j).
O(n²) because total prints are 1+2+…+n = n(n+1)/2.

Explore More Pattern Variations

Repeating patterns are a great stepping stone to Floyd’s triangle and other numeric layouts.

All Number Patterns →
Did you know?

Repeating patterns are often the simplest way to test whether you really understand the difference between row index (i) and column index (j).

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