Sequential Number Triangle in C

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

What You’ll Learn

How to print a sequential number triangle in C using a single running counter k. Each row prints the next values in sequence while the row length decreases.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
  1  2  3  4  5
  6  7  8  9
 10 11 12
 13 14
 15
1

Complete C Program

Use k++ to keep numbers continuous. Print each row with %3d to keep spacing aligned.

c
#include <stdio.h>

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

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

    return 0;
}

🧠 How It Works

1

Initialize the counter

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

Counter
2

Outer loop controls the rows

for (i = 1; i <= rows; ++i) runs once per row.

Row control
3

Inner loop prints decreasing row length

for (j = rows; j >= i; --j) prints rows - i + 1 numbers in that row.

Printing
4

Format with %3d

printf("%3d", k++) keeps spacing consistent so columns visually line up.

Alignment
=

Sequential triangle

Total printed numbers are 1+2+…+n = n(n+1)/2, so time complexity is O(n²) for n rows.

2

Variation — User Input Version

Accept the row count at runtime. Keep the same counter idea:

c
#include <stdio.h>

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

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

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

    return 0;
}

💡 Tips for Enhancement

Try These

  • Change spacing with %2d, %4d, or %-3d (left-align)
  • Start from any value by setting k to a different number
  • Print the reverse sequence by starting k at the total count and decrementing
  • Replace numbers with letters to build sequential alphabet triangles

Avoid

  • Forgetting to increment the counter (k++)
  • Mixing row logic and print logic (keep rows in the outer loop)
  • Removing %3d spacing if you still want alignment

Key Takeaways

1

A single counter k keeps numbers continuous across rows.

2

Row length decreases because the inner loop runs from rows down to i.

3

%3d formatting improves visual alignment.

4

Total printed values are n(n+1)/2 for n rows.

❓ Frequently Asked Questions

Because we print k and immediately increment it (k++) after every number, so the next print continues the sequence.
It prints each integer in a 3-character wide field, right-aligned, so the spacing stays consistent even when values reach two digits.
Replace printf("%3d", k++) with printf("%d ", k++). The triangle will still be correct but columns may not line up neatly.
O(n²) for n rows, since total prints are n(n+1)/2.

Explore More C Number Patterns!

Keep practicing loops with more triangles, pyramids, and number sequences.

All Number Patterns →
Did you know?

The numbers printed in each row are consecutive because k increments once per print. For rows = 5, you print 15 values in total.

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.

6 people found this page helpful