Sequential Number Triangle in C

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:
1 2 3 4 5
6 7 8 9
10 11 12
13 14
15Complete C Program
Use k++ to keep numbers continuous. Print each row with %3d to keep spacing aligned.
#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
Initialize the counter
int k = 1; stores the next number to print.
Outer loop controls the rows
for (i = 1; i <= rows; ++i) runs once per row.
Inner loop prints decreasing row length
for (j = rows; j >= i; --j) prints rows - i + 1 numbers in that row.
Format with %3d
printf("%3d", k++) keeps spacing consistent so columns visually line up.
Sequential triangle
Total printed numbers are 1+2+…+n = n(n+1)/2, so time complexity is O(n²) for n rows.
Variation — User Input Version
Accept the row count at runtime. Keep the same counter idea:
#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
kto a different number - Print the reverse sequence by starting
kat 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
%3dspacing if you still want alignment
Key Takeaways
A single counter k keeps numbers continuous across rows.
Row length decreases because the inner loop runs from rows down to i.
%3d formatting improves visual alignment.
Total printed values are n(n+1)/2 for n rows.
❓ Frequently Asked Questions
k and immediately increment it (k++) after every number, so the next print continues the sequence.printf("%3d", k++) with printf("%d ", k++). The triangle will still be correct but columns may not line up neatly.n(n+1)/2.Explore More C Number Patterns!
Keep practicing loops with more triangles, pyramids, and number sequences.
The numbers printed in each row are consecutive because k increments once per print. For rows = 5, you print 15 values in total.
6 people found this page helpful
