Continuous Number Triangle in C

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

What You’ll Learn

How to print a continuous number triangle in C. Each row prints one more number than the previous row, and the numbers continue sequentially (they do not reset at each new line).

The key idea is using a separate variable like number that increments every time you print.

⭐ Pattern Output

For rows = 4, the pattern looks like this:

Output
1
2 3
4 5 6
7 8 9 10
1

Complete C Program

The inner loop prints i numbers on row i. A counter variable keeps the sequence continuous across rows.

c
#include <stdio.h>

int main() {
    int rows = 4;
    int number = 1;
    int i, j;

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

    return 0;
}

🧠 How It Works

1

Initialize the counter

int number = 1; stores the next value to print.

State
2

Outer loop controls rows

for (i = 1; i <= rows; ++i) prints one row per iteration.

Row control
3

Inner loop prints i numbers

for (j = 1; j <= i; ++j) prints exactly i values on row i.

Printing
4

Increment after each print

After printing, do ++number so the next print continues the sequence.

Continuity
=

Continuous sequence across rows

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

2

Variation — User Input Version

Read the number of rows from the user using scanf():

c
#include <stdio.h>

int main() {
    int rows;
    int number = 1;
    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 ", number);
            ++number;
        }
        printf("\n");
    }

    return 0;
}

💡 Tips for Enhancement

Try These

  • Start from another value by changing number (e.g., 10)
  • Print only even numbers using number += 2
  • Format columns with fixed width (use printf("%3d", number))
  • Right-align the triangle by printing leading spaces
  • Switch to characters for a continuous alphabet triangle

Avoid

  • Resetting number inside the outer loop (it breaks continuity)
  • Forgetting the newline after each row
  • Using negative row values without validation
  • Mixing row/column logic (keep the inner loop for columns)
  • Assuming the output will align without consistent spacing

Key Takeaways

1

A separate counter variable keeps the numbers continuous across rows.

2

Row i prints exactly i values.

3

Total prints is n(n+1)/2, so time complexity is O(n²).

4

This counter technique applies to many patterns (numbers, letters, Fibonacci, etc.).

❓ Frequently Asked Questions

Because number is declared outside the loops and is incremented after each print, so it keeps its value for the next row.
Yes. Replace printf("%d ", number) with printf("%d", number), but multi-digit numbers can merge visually (e.g., 910). Spaces improve readability.
Initialize the counter as int number = 10; instead of 1.
O(n²) for n rows: total prints are 1+2+…+n = n(n+1)/2.

Explore More C Number Patterns!

Keep going with more triangles and pyramids that strengthen your loop fundamentals.

All Number Patterns →
Did you know?

This pattern is the base for many interview questions: if you can maintain a counter across nested loops, you can generate sequences like odd-only, even-only, Fibonacci, and more.

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