Pattern Programs in C (Nested Loops & Star Patterns)

Pattern programs are classic exercises for mastering nested loops, loop boundary indexing, and formatted terminal output in C.
Mastering nested for-loops is essential for multidimensional arrays and matrix manipulation.
This guide covers right-half pyramids, centered full pyramids, and Floyd's consecutive number triangle.
Interactive Runnable C Implementation
#include <stdio.h>
void print_right_pyramid(int rows) {
printf("1. Right-Half Star Pyramid (Rows = %d):\n", rows);
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
printf("* ");
}
printf("\n");
}
}
void print_full_pyramid(int rows) {
printf("\n2. Full Centered Star Pyramid (Rows = %d):\n", rows);
for (int i = 1; i <= rows; i++) {
for (int space = 1; space <= rows - i; space++) {
printf(" ");
}
for (int j = 1; j <= 2 * i - 1; j++) {
printf("* ");
}
printf("\n");
}
}
void print_floyd_triangle(int rows) {
printf("\n3. Floyd's Number Triangle (Rows = %d):\n", rows);
int num = 1;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
printf("%-3d", num++);
}
printf("\n");
}
}
int main(void) {
int rows = 5;
print_right_pyramid(rows);
print_full_pyramid(rows);
print_floyd_triangle(rows);
return 0;
}
Expected Output
1. Right-Half Star Pyramid (Rows = 5):
*
* *
* * *
* * * *
* * * * *
2. Full Centered Star Pyramid (Rows = 5):
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
3. Floyd's Number Triangle (Rows = 5):
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Frequently Asked Questions
How do you center an odd number of stars per row?
Print (rows - i) double spaces, then print (2 * i - 1) stars with a space delimiter.