Incremental Number Triangle in C

What You’ll Learn
How to print an incremental number triangle where each row starts with the row index and each next value is computed using a decreasing increment.
⭐ Pattern Output
For rows = 5, the pattern looks like this:
1
2 6
3 7 10
4 8 11 13
5 9 12 14 15Complete C Program
We print one row at a time. After printing a value, we update it with currNum += (rows - j).
#include <stdio.h>
int main() {
int rows = 5;
int num = 1;
for (int i = 1; i <= rows; i++) {
int currNum = num;
for (int j = 1; j <= i; j++) {
printf("%d ", currNum);
currNum += (rows - j);
}
printf("\n");
num++;
}
return 0;
}🧠 How It Works
Set rows and starting value
rows controls the height and num controls the first number of each row.
Start each row with currNum
currNum = num ensures row 1 starts at 1, row 2 at 2, etc.
Decreasing increment across the row
After each print we add (rows - j). With rows=5 this produces 4,3,2,1….
Incremental triangle
This is a good exercise for combining nested loops with arithmetic sequences.
Variation — User Input Version
Read the number of rows using scanf().
#include <stdio.h>
int main() {
int rows;
int num = 1;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (int i = 1; i <= rows; i++) {
int currNum = num;
for (int j = 1; j <= i; j++) {
printf("%d ", currNum);
currNum += (rows - j);
}
printf("\n");
num++;
}
return 0;
}💡 Tips for Enhancement
Try These
- Change the increment formula to generate new sequences
- Use formatted printing to align columns
- Compute and print each row’s sum
Avoid
- Using
rows <= 0without validating input - Forgetting to reset
currNumat the start of each row
Key Takeaways
Row i prints i values, forming a triangle.
Each row starts with the row number.
The increment decreases across the row via (rows - j).
Total work is 1+2+…+n, so runtime is O(n²).
❓ Frequently Asked Questions
(rows - j), and as j increases, rows - j decreases.rows or use the input version and enter a larger number.n rows.Explore More C Number Patterns!
Try other arithmetic and triangle patterns to strengthen your loop skills.
The update currNum += (rows - j) is an easy way to create a decreasing-step arithmetic progression across each row.
12 people found this page helpful
