Diagonal Increment Number Triangle in C

What You’ll Learn
How to print a diagonal increment number triangle in C. Each row starts with its row number, then continues by adding a decreasing increment \((rows - j)\).
This pattern is a great way to practice nested loops and custom increment rules inside the inner loop.
⭐ 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 set currentNumber to the row start, print it, then add rows - j each step to get the next value.
#include <stdio.h>
int main() {
int rows = 5;
int start = 1;
int i, j;
for (i = 1; i <= rows; ++i) {
int currentNumber = start;
for (j = 1; j <= i; ++j) {
printf("%d ", currentNumber);
currentNumber += rows - j;
}
printf("\n");
++start;
}
return 0;
}🧠 How It Works
Pick the row count
rows = 5 sets the triangle height and drives the increment formula.
Start value per row
We use start to track the first number of each row: row 1 starts at 1, row 2 starts at 2, etc.
Decreasing increment rule
After printing a value, we do currentNumber += rows - j. When j increases, \((rows - j)\) decreases, so the increments shrink as you move right.
Diagonal-like progression
Total prints are 1+2+…+n = n(n+1)/2, so time complexity is O(n²).
Variation — User Input Version
Make it dynamic by reading the number of rows from the user:
#include <stdio.h>
int main() {
int rows;
int start = 1;
int i, j;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i) {
int currentNumber = start;
for (j = 1; j <= i; ++j) {
printf("%d ", currentNumber);
currentNumber += rows - j;
}
printf("\n");
++start;
}
return 0;
}💡 Tips for Enhancement
Try These
- Make increments increase by using
currentNumber += j - Print aligned columns using a fixed width format like
printf("%3d", currentNumber) - Change the starting value to shift the whole triangle
- Create a right-aligned version by printing leading spaces
- Convert to characters for a diagonal alphabet pattern
Avoid
- Resetting
startinside the outer loop - Using the wrong increment formula (it changes the pattern)
- Forgetting the newline between rows
- Allowing invalid rows without basic validation
- Printing without spacing if you want multi-digit readability
Key Takeaways
Each row starts with its row number (tracked via start).
Next values are generated using the decreasing increment \((rows - j)\).
Nested loops define the triangle shape; the increment rule defines the diagonal effect.
Total work is O(n²) for n rows.
❓ Frequently Asked Questions
rows - j. As j grows (moving right), the value of rows - j becomes smaller.currentNumber += rows - j with currentNumber += j.Explore More C Number Patterns!
Keep practicing with patterns that use custom rules to generate each next value.
Changing only the inner-loop update rule can generate dozens of unique number patterns—the triangle shape stays the same, but the sequence behavior changes.
12 people found this page helpful
