Pattern programs are useful for learning nested loops. The outer loop usually controls the row, while an inner loop prints spaces, symbols or numbers inside that row.
#include <stdio.h>
int main(void) {
for (int i = 0; i < 5; i++) {
putchar('*');
}
putchar('\n');
return 0;
}
Output
*****
#include <stdio.h>
int main(void) {
for (int row = 5; row >= 1; row--) {
for (int column = 1; column <= row; column++) {
putchar('*');
}
putchar('\n');
}
return 0;
}
Output
*****
****
***
**
*
#include <stdio.h>
int main(void) {
for (int row = 1; row <= 5; row++) {
for (int column = 1; column <= row; column++) {
putchar('*');
}
putchar('\n');
}
return 0;
}
Output
*
**
***
****
*****
#include <stdio.h>
int main(void) {
const int rows = 5;
for (int row = 1; row <= rows; row++) {
for (int space = row; space < rows; space++) {
putchar(' ');
}
for (int star = 1; star <= 2 * row - 1; star++) {
putchar('*');
}
putchar('\n');
}
return 0;
}
Output
*
***
*****
*******
*********
#include <stdio.h>
int main(void) {
const int rows = 5;
for (int row = rows; row >= 1; row--) {
for (int space = row; space < rows; space++) {
putchar(' ');
}
for (int star = 1; star <= 2 * row - 1; star++) {
putchar('*');
}
putchar('\n');
}
return 0;
}
Output
*********
*******
*****
***
*
#include <stdio.h>
int main(void) {
for (int row = 1; row <= 5; row++) {
for (int number = 1; number <= row; number++) {
printf("%d", number);
}
putchar('\n');
}
return 0;
}
Output
1
12
123
1234
12345
#include <stdio.h>
int main(void) {
for (int row = 1; row <= 5; row++) {
for (int column = 1; column <= row; column++) {
printf("%d", row);
}
putchar('\n');
}
return 0;
}
Output
1
22
333
4444
55555
#include <stdio.h>
int main(void) {
const int rows = 4;
for (int row = 1; row <= rows; row++) {
for (int space = row; space < rows; space++) {
putchar(' ');
}
for (int star = 1; star <= 2 * row - 1; star++) {
putchar('*');
}
putchar('\n');
}
for (int row = rows - 1; row >= 1; row--) {
for (int space = row; space < rows; space++) {
putchar(' ');
}
for (int star = 1; star <= 2 * row - 1; star++) {
putchar('*');
}
putchar('\n');
}
return 0;
}
Output
*
***
*****
*******
*****
***
*
Review for loops and while loops before creating more complex patterns.
The original page contained several less-common double-sided patterns in addition to the standard pyramids. This keeps that exercise family instead of replacing it only with generic patterns.
#include <stdio.h>
int main(void) {
const int width = 5;
for (int row = width; row >= 1; row--) {
for (int i = 0; i < row; i++) putchar('*');
for (int gap = 0; gap < 2 * (width - row) + 1; gap++) putchar(' ');
for (int i = 0; i < row; i++) putchar('*');
putchar('\\n');
}
return 0;
}
#include <stdio.h>
int main(void) {
const int width = 5;
for (int row = 1; row <= width; row++) {
for (int i = 0; i < row; i++) putchar('*');
for (int gap = 0; gap < 2 * (width - row) + 1; gap++) putchar(' ');
for (int i = 0; i < row; i++) putchar('*');
putchar('\\n');
}
return 0;
}
#include <stdio.h>
void row(int stars, int width) {
for (int i = 0; i < stars; i++) putchar('*');
for (int gap = 0; gap < 2 * (width - stars) + 1; gap++) putchar(' ');
for (int i = 0; i < stars; i++) putchar('*');
putchar('\\n');
}
int main(void) {
const int width = 5;
for (int stars = width; stars >= 1; stars--) row(stars, width);
for (int stars = 2; stars <= width; stars++) row(stars, width);
return 0;
}
#include <stdio.h>
int main(void) {
for (int step = 1; step < 10; step++) {
for (int value = 1; value < 10; value += step) {
printf("%d ", value);
}
putchar('\n');
}
return 0;
}
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.