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

What You’ll Learn
How to print a left-shifted sequential number triangle in C that starts from 1. Each row begins one number later than the previous row, and values in the row increase sequentially.
It’s a clean nested-loop pattern: the outer loop controls rows, and the inner loop prints i+1 numbers in each row.
⭐ Pattern Output
For rows = 5 and start_num = 1, the pattern looks like this:
1
2 3
3 4 5
4 5 6 7
5 6 7 8 9Complete C Program
We compute num = start_num + i at the start of each row, then print i+1 values while incrementing num.
#include <stdio.h>
int main() {
int rows = 5;
int start_num = 1;
for (int i = 0; i < rows; i++) {
int num = start_num + i;
for (int j = 0; j <= i; j++) {
printf("%d ", num);
num++;
}
printf("\n");
}
return 0;
}🧠 How It Works
Set rows and start value
We pick rows = 5 and set start_num = 1.
Outer loop controls rows
for (i = 0; i < rows; i++) decides which row we are printing.
Shift the row start
We compute num = start_num + i so the row starts become 1, 2, 3, 4, 5.
Inner loop prints i+1 values
for (j = 0; j <= i; j++) prints 1 value on the first row, 2 values on the second row, and so on. We increment num++ after each print.
Left-shifted sequential triangle (from 1)
Total printed values: 1+2+…+n = n(n+1)/2, so time complexity is O(n²) for n rows.
Variation — User Input Version
Accept both the row count and the starting number using scanf():
#include <stdio.h>
int main() {
int rows, start_num;
printf("Enter the number of rows: ");
scanf("%d", &rows);
printf("Enter the starting number: ");
scanf("%d", &start_num);
for (int i = 0; i < rows; i++) {
int num = start_num + i;
for (int j = 0; j <= i; j++) {
printf("%d ", num);
num++;
}
printf("\n");
}
return 0;
}💡 Tips for Enhancement
Try These
- Change
start_numto generate a new starting point - Use
printf("%02d ", num)for aligned columns - Remove trailing spaces by printing a space only when
j < i - Create a right-aligned version by printing leading spaces per row
- Replace numbers with letters to create alphabet patterns
Avoid
- Forgetting
printf("\n")after each row - Using invalid input values without validation
- Not resetting
numat the beginning of each row - Swapping loop roles (outer loop rows, inner loop columns)
Key Takeaways
The outer loop sets the row index and row length.
The row start is computed as start_num + i.
The inner loop prints sequential values by incrementing num.
Overall runtime grows as O(n²) with the number of rows.
❓ Frequently Asked Questions
start_num = 1. Each row will then start at 1+i.i = 1, we compute num = 1 + 1 which is 2.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.
When you print n rows, you print n(n+1)/2 values in total. That’s why these nested-loop patterns often have O(n²) runtime.
12 people found this page helpful
