Left-Shifted Sequential Number Triangle (Start from 0) in C

What You’ll Learn
How to print a left-shifted sequential number triangle in C that starts from 0. Each row begins with its row index (0, 1, 2, ...), then continues sequentially across the row.
This is a nice example of using nested loops and resetting a row variable (num) at the start of each row.
⭐ Pattern Output
For rows = 6, the pattern looks like this:
0
1 2
2 3 4
3 4 5 6
4 5 6 7 8
5 6 7 8 9 10Complete C Program
We start each row with num = i (the row index). Then we print i+1 values while incrementing num.
#include <stdio.h>
int main() {
int rows = 6;
for (int i = 0; i < rows; i++) {
int num = i;
for (int j = 0; j <= i; j++) {
printf("%d ", num);
num++;
}
printf("\n");
}
return 0;
}🧠 How It Works
Choose the number of rows
rows = 6 prints 6 lines (0..5 as row indices).
Outer loop controls the row index
i goes from 0 to 5 and determines the row length (i+1 values).
Start each row at i
We set num = i, so the first value in each row equals the row index.
Inner loop prints sequential values
The inner loop prints i+1 values, incrementing num++ after each print.
Left-shifted sequential triangle (from 0)
Total printed values: 1+2+…+n = n(n+1)/2, so time complexity is O(n²) for n rows.
Variation — User Input Version
Accept the number of rows using scanf():
#include <stdio.h>
int main() {
int rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (int i = 0; i < rows; i++) {
int num = i;
for (int j = 0; j <= i; j++) {
printf("%d ", num);
num++;
}
printf("\n");
}
return 0;
}💡 Tips for Enhancement
Try These
- Use an offset like
num = i + 10to start from 10 - Print without trailing spaces by handling the last value in each row
- Use
printf("%02d ", num)for equal-width columns - Create a right-aligned version by adding leading spaces
- Swap numbers for letters to build alphabet variations
Avoid
- Forgetting
printf("\n")after each row - Not resetting
numat the beginning of each row - Mixing row and column logic (outer loop rows, inner loop columns)
- Using negative row counts without input validation
Key Takeaways
The outer loop picks the row index i and row length (i+1 values).
Setting num = i makes each row start at its row index.
The inner loop prints sequential values by incrementing num.
Time complexity is O(n²) for n rows.
❓ Frequently Asked Questions
i = 0, so the inner loop runs once and prints num = 0.int num = i + 10; at the start of each row.j < i, or print the last value separately without a trailing space.Explore More C Number Patterns!
Keep practicing nested loops with more number pattern variations—from triangles to pyramids.
For n rows, you print n(n+1)/2 values in total. That sum is why many triangle patterns naturally have O(n²) runtime.
12 people found this page helpful
