Print a Full Pyramid Number Pattern in C
To print a full pyramid number pattern in C, we use nested loops: the outer loop controls the rows, while the inner loops manage spaces and numbers. The pyramid consists of centered numbers, increasing from the top to the bottom.
Examples of Full Pyramid Number Patterns
1. Basic Full Pyramid Number Pattern
In this example, we will print a simple full pyramid number pattern where numbers increase from left to right in each row.
main.c
</>
Copy
#include <stdio.h>
int main() {
int rows = 5;
for (int i = 1; i <= rows; i++) {
// Print spaces
for (int j = i; j < rows; j++) {
printf(" ");
}
// Print numbers
for (int k = 1; k <= (2 * i - 1); k++) {
printf("%d", k);
}
printf("\n");
}
return 0;
}
Explanation:
- We define
rows = 5
to set the number of pyramid levels. - The outer loop
for (int i = 1; i <= rows; i++)
iterates over the rows. Refer C For Loop. - The first inner loop prints spaces before the numbers to align them.
- The second inner loop prints numbers in an increasing sequence up to
(2*i - 1)
to form the pyramid. - After each row,
printf("\n")
moves to the next line.
Output:
1
123
12345
1234567
123456789
2. Full Pyramid with Repeated Row Numbers
In this example, each row contains repeated numbers instead of increasing numbers.
main.c
</>
Copy
#include <stdio.h>
int main() {
int rows = 5;
for (int i = 1; i <= rows; i++) {
// Print spaces
for (int j = i; j < rows; j++) {
printf(" ");
}
// Print repeated row numbers
for (int k = 1; k <= (2 * i - 1); k++) {
printf("%d", i);
}
printf("\n");
}
return 0;
}
Explanation:
- We set
rows = 5
to create five levels. - The outer loop iterates from 1 to
rows
to print each row. - The first inner loop prints spaces to center the numbers.
- The second inner loop prints the row number multiple times instead of increasing numbers.
- Each row ends with
printf("\n")
to move to the next level.
Output:
1
222
33333
4444444
555555555
3. Full Pyramid with Inverted Numbering
In this example, we print a pyramid where each row contains numbers in a descending sequence.
main.c
</>
Copy
#include <stdio.h>
int main() {
int rows = 5;
for (int i = 1; i <= rows; i++) {
// Print spaces
for (int j = i; j < rows; j++) {
printf(" ");
}
// Print descending numbers
for (int k = i; k >= 1; k--) {
printf("%d", k);
}
for (int l = 2; l <= i; l++) {
printf("%d", l);
}
printf("\n");
}
return 0;
}
Explanation:
- We set
rows = 5
to determine the height of the pyramid. - The outer loop runs from
1
torows
. - The first inner loop prints spaces for alignment.
- The second inner loop prints numbers in descending order from
i
to1
. - The third inner loop prints numbers in ascending order from
2
toi
. - Each row is printed on a new line using
printf("\n")
.
Output:
1
212
32123
4321234
543212345
Conclusion
In this tutorial, we covered different ways to print a full pyramid number pattern in C:
- Basic Full Pyramid: Increasing numbers in each row.
- Repeated Row Numbers: Each row consists of the same digit.
- Inverted Numbering: Numbers decrease to 1, then increase again.