Odd-Length Descending Number Pyramid in C

What You’ll Learn
How to print an odd-length descending number pyramid in C. Each row prints numbers starting at 1, but the row length shrinks by 2 each line.
The key is the formula 2*i - 1, which produces odd row lengths like 7, 5, 3, and 1.
⭐ Pattern Output
For rows = 4, the pattern looks like this:
Output
1234567
12345
123
11
Complete C Program
The outer loop decreases the row size, and the inner loop prints from 1 up to 2*i - 1.
c
#include <stdio.h>
int main() {
int rows = 4;
int i, j;
for (i = rows; i >= 1; --i) {
for (j = 1; j <= (2 * i - 1); ++j) {
printf("%d", j);
}
printf("\n");
}
return 0;
}2
Variation — User Input Version
Accept the number of rows from the user:
c
#include <stdio.h>
int main() {
int rows;
int i, j;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = rows; i >= 1; --i) {
for (j = 1; j <= (2 * i - 1); ++j) {
printf("%d", j);
}
printf("\n");
}
return 0;
}Explore More Number Patterns
Try switching 2*i - 1 to 2*i for even-length rows, or reverse the outer loop for a growing pyramid.
Did you know?
The sum of the first n odd numbers is n². That’s why this pattern prints exactly rows² digits in total.
12 people found this page helpful
