C Program to Print Pyramids and Patterns
In this example, we will learn to print half pyramids, inverted pyramids, full pyramids, inverted full pyramids, Pascal's triangle, and Floyd's triangle.
Remainder :
To understand this example, we should have the knowledge of the following C programming topics:
Half Pyramid of Stars (*)
#include <stdio.h>
int main() {
int i, j, rows;
printf("Enter the number of rows : ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i) {
for (j = 1; j <= i; ++j) {
printf("* ");
}
printf("\n");
}
return 0;
}
Output
Enter the number of rows : 6
*
* *
* * *
* * * *
* * * * *
* * * * * *
Half Pyramid of Numbers
#include <stdio.h>
int main() {
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i) {
for (j = 1; j <= i; ++j) {
printf("%d ", j);
}
printf("\n");
}
return 0;
}
Output
Enter the number of rows: 6
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
Half Pyramid of Alphabets
#include <stdio.h>
#include <stdio.h>
int main() {
int i, j;
char input, alphabet = 'A';
printf("Enter a character to print at last : ");
scanf("%c", &input);
input = toupper(input);
for (i = 1; i <= (input - 'A' + 1); ++i) {
for (j = 1; j <= i; ++j) {
printf("%c ", alphabet);
}
++alphabet;
printf("\n");
}
return 0;
}
Output
Enter a character to print at last : f
A
B B
C C C
D D D D
E E E E E
F F F F F F
Inverted half pyramid of Stars (*)
#include <stdio.h>
int main() {
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = rows; i >= 1; i--) {
for (j = 1; j <= i; j++) {
printf("* ");
}
printf("\n");
}
return 0;
}
Output
Enter the number of rows: 6
* * * * * *
* * * * *
* * * *
* * *
* *
*
Inverted half pyramid of numbers
#include <stdio.h>
int main() {
int i, j, rows;
printf("Enter the number of rows : ");
scanf("%d", &rows);
for (i = rows; i >= 1; i--) {
for (j = 1; j <= i; j++) {
printf("%d ", j);
}
printf("\n");
}
return 0;
}
Output
Enter the number of rows : 6
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
Full Pyramid of Star(*)
#include <stdio.h>
int main() {
int i, space, rows = 0, j = 0;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i, j = 0) {
for (space = 1; space <= rows - i; ++space) {
printf(" ");
}
while (j != 2 * i - 1) {
printf("* ");
++j;
}
printf("\n");
}
return 0;
}
Output
Enter the number of rows: 6
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
* * * * * * * * * * *
Full Pyramid of Numbers
#include <stdio.h>
int main() {
int i, space, rows, j = 0, count = 0, count1 = 0;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i) {
for (space = 1; space <= rows - i; ++space) {
printf(" ");
++count;
}
while (j != 2 * i - 1) {
if (count <= rows - 1) {
printf("%d ", i + j);
++count;
} else {
++count1;
printf("%d ", (i + j - 2 * count1));
}
++j;
}
count1 = count = j = 0;
printf("\n");
}
return 0;
}
Output
Enter the number of rows: 5
1
2 3 2
3 4 5 4 3
4 5 6 7 6 5 4
5 6 7 8 9 8 7 6 5
Inverted full pyramid of Stars(*)
#include <stdio.h>
int main() {
int i, j, rows, space;
printf("Enter the number of rows : ");
scanf("%d", &rows);
for (i = rows; i >= 1; i--) {
for (space = 0; space < rows - i; ++space)
printf(" ");
for (j = i; j <= 2 * i - 1; j++)
printf("* ");
for (j = 0; j < i - 1; j++)
printf("* ");
printf("\n");
}
return 0;
}
Output
Enter the number of rows : 6
* * * * * * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
Pascal's Triangle
What is a Pascal Triangle?
Pascal’s Triangle is a kind of number pattern. The numbers are so arranged that they reflect as a triangle.
To build the triangle, start with "1" at the top, then continue placing numbers below it in a triangular pattern. Each number is the numbers directly above it added together.
#include <stdio.h>
int main() {
int i, j, rows, k = 1, space;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 0; i < rows; i++) {
for (space = 1; space <= rows - i; space++)
printf(" ");
for (j = 0; j <= i; j++) {
if (j == 0 || i == 0)
k = 1;
else
k = k * (i - j + 1) / j;
printf("%4d", k);
}
printf("\n");
}
return 0;
}
Output
Enter the number of rows: 6
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
Floyd's Triangle
What is a Floyd's Triangle?
Floyd's triangle is a right angled triangle, which is made using natural numbers. It starts from 1 and consecutively selects the next greater number in sequence.
#include <stdio.h>
int main() {
int rows, i, j, num = 1;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; i++) {
for (j = 1; j <= i; ++j) {
printf("%d ", num);
++num;
}
printf("\n");
}
return 0;
}
Output
Enter the number of rows: 6
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21