Continuous Number Triangle in C

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:
1
2 3
4 5 6
7 8 9 10Complete C Program
The inner loop prints i numbers on row i. A counter variable keeps the sequence continuous across rows.
#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
Initialize the counter
int number = 1; stores the next value to print.
Outer loop controls rows
for (i = 1; i <= rows; ++i) prints one row per iteration.
Inner loop prints i numbers
for (j = 1; j <= i; ++j) prints exactly i values on row i.
Increment after each print
After printing, do ++number so the next print continues the sequence.
Continuous sequence across rows
Total prints is 1+2+…+n = n(n+1)/2, so time complexity is O(n²).
Variation — User Input Version
Read the number of rows from the user using scanf():
#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
numberinside 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
A separate counter variable keeps the numbers continuous across rows.
Row i prints exactly i values.
Total prints is n(n+1)/2, so time complexity is O(n²).
This counter technique applies to many patterns (numbers, letters, Fibonacci, etc.).
❓ Frequently Asked Questions
number is declared outside the loops and is incremented after each print, so it keeps its value for the next row.printf("%d ", number) with printf("%d", number), but multi-digit numbers can merge visually (e.g., 910). Spaces improve readability.int number = 10; instead of 1.Explore More C Number Patterns!
Keep going with more triangles and pyramids that strengthen your loop fundamentals.
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.
12 people found this page helpful
