Left-Shifted Sequential Number Triangle in C

What You’ll Learn
How to print a left-shifted sequential number triangle in C. Each next row starts with a number that is one higher than the previous row, while values inside a row increase sequentially.
This is a great exercise to practice how the outer loop controls rows and the inner loop controls how many numbers appear per row.
⭐ Pattern Output
For rows = 5 and start_num = 11, the pattern looks like this:
11
12 13
13 14 15
14 15 16 17
15 16 17 18 19Complete C Program
For each row i, we set num = start_num + i (so each row starts one number later), then print i+1 values while incrementing num.
#include <stdio.h>
int main() {
int rows = 5;
int start_num = 11;
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 starting number
We define how many lines to print and the first value of row 1: rows = 5, start_num = 11.
Outer loop controls the rows
for (i = 0; i < rows; i++) prints rows from 0 to 4 (total 5 rows).
Start each row one number later
We compute the row start as num = start_num + i. That gives 11, 12, 13, 14, 15 for rows 1..5.
Inner loop prints i+1 values
for (j = 0; j <= i; j++) prints 1 value on row 1, 2 values on row 2, etc. After printing one value, we do num++.
Left-shifted sequential triangle
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 sequence - Print without trailing spaces by handling the last print in each row
- Use
printf("%02d ", num)to keep equal-width columns - Right-align the triangle by printing leading spaces per row
- Swap numbers for characters to build alphabet variations
Avoid
- Forgetting
printf("\n")after each row - Letting
rowsbe zero/negative without validating input - Resetting
numincorrectly (the row must start atstart_num + i) - Mixing row and column conditions (outer loop for rows, inner loop for columns)
Key Takeaways
The outer loop chooses the row and its length (i+1 values).
Row starts are computed using start_num + i to create the left-shift.
The inner loop prints sequential values by incrementing num.
Time complexity is O(n²) for n rows.
❓ Frequently Asked Questions
num = start_num + i, and i increases by 1 for each next row.j < i, or print the last value separately without a trailing space.start_num = 1. The rest of the code remains the same.Explore More C Number Patterns!
Keep practicing nested loops with more number pattern variations—from triangles to pyramids.
For n rows, you print 1+2+…+n values in total. That sum is n(n+1)/2, which is why many pattern programs naturally have O(n²) runtime.
12 people found this page helpful
