Odd-Length Consecutive Number Pyramid in C

What You’ll Learn
How to print an odd-length consecutive number pyramid in C. Each row prints consecutive digits starting from 1, but the row length increases by 2 each time: 1, 3, 5, 7, 9…
The key idea is using i += 2 in the outer loop to generate only odd row lengths.
⭐ Pattern Output
For a maximum width of 9, the pattern looks like this:
1
123
12345
1234567
123456789Complete C Program
The outer loop picks the row length (odd numbers only). The inner loop prints 1..i in each row.
#include <stdio.h>
int main() {
int i, j;
for (i = 1; i <= 9; i += 2) {
for (j = 1; j <= i; ++j) {
printf("%d", j);
}
printf("\n");
}
return 0;
}🧠 How It Works
Use odd row lengths
for (i = 1; i <= 9; i += 2) produces 1, 3, 5, 7, 9.
Print 1..i each row
for (j = 1; j <= i; ++j) prints consecutive digits starting from 1.
Move to next line
printf("\\n") ends the current row.
Odd-length pyramid
Total prints are 1+3+5+…+n, which grows on the order of n², so time complexity is O(n²).
Variation — User Input Version
Let the user choose the maximum width. If the user enters an even number, you can still print odd rows up to it (or subtract 1).
#include <stdio.h>
int main() {
int max;
int i, j;
printf("Enter the maximum width (odd number recommended): ");
scanf("%d", &max);
for (i = 1; i <= max; i += 2) {
for (j = 1; j <= i; ++j) {
printf("%d", j);
}
printf("\n");
}
return 0;
}💡 Tips for Enhancement
Try These
- Print even-length rows by starting at
2and usingi += 2 - Add spaces with
printf("%d ", j) - Right-align by printing leading spaces before each row
- Change the start digit to shift the pyramid
- Replace digits with letters for an alphabet pyramid
Avoid
- Using
i++(it breaks the odd-length requirement) - Forgetting the newline between rows
- Printing without spacing when multi-digit output is expected
- Assuming max is odd if you accept user input
- Mixing row/column roles in your loops
Key Takeaways
Use i += 2 to generate odd row lengths (1, 3, 5…).
Each row prints consecutive digits from 1 to i.
The same structure works for even-length or spaced variants.
Total operations grow on the order of n².
❓ Frequently Asked Questions
2 and still add 2 each time: for (i = 2; i <= max; i += 2).j from 1 up to the row length i.Explore More C Number Patterns!
Practice with pyramids and triangles that change row length rules.
The sum of the first \(k\) odd numbers is \(k^2\). That’s why printing 1, 3, 5, 7, 9 values grows like a square as the pyramid widens.
12 people found this page helpful
