Pyramid pattern printing is a very popular logical program & It enhanced your logical thinking. In this article, we will learn how to make different types of pyramid patterns in c programming.
Pyramid Pattern
A pyramid is a 3 – dimensional shape that has a polygon base and the sides of the pyramid look like a triangle shape. Pyramid pattern printing is a logical program by which we can develop logical thinking in programming. We can use conditional statements and loop concepts for making pyramid patterns. Pyramid patterns can be printed using numbers, characters, and special characters like stars.
we will make 4 types of pyramid patterns.
- Half Pyramid of Numbers
- Inverted Half Pyramid of Numbers
- Full Pyramid of Numbers
- Full Pyramid of Numbers in 180 Degree
1. Half Pyramid Pattern of Numbers
Half pyramid pattern of numbers looks like a right-angle triangle of numbers in which the hypotenuse is on the right side.
Examples:
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
C Program to Print Half Pyramid Pattern of Numbers
C
#include <stdio.h>
int main()
{
int rows;
printf ( "Number of rows: " );
scanf ( "%d" , &rows);
for ( int i = 1; i <= rows; i++) {
for ( int j = 1; j <= i; j++) {
printf ( "%d " , i);
}
printf ( "\n" );
}
return 0;
}
|
Output :
Number of rows: 5
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
2. Inverted Half Pyramid of Numbers
An inverted half-pyramid pattern of numbers is 180° reversed version of half pyramid pattern.
Examples:
5 5 5 5 5
4 4 4 4
3 3 3
2 2
1
C Program to Print Inverted Half Pyramid Pattern of Numbers
C
#include <stdio.h>
int main()
{
int rows;
printf ( "Number of rows: " );
scanf ( "%d" , &rows);
for ( int i = rows; i >= 1; i--) {
for ( int j = 1; j <= i; j++) {
printf ( "%d " , i);
}
printf ( "\n" );
}
return 0;
}
|
Output :
Number of rows: 5
5 5 5 5 5
4 4 4 4
3 3 3
2 2
1
3. Full Pyramid Pattern of Numbers
A full pyramid pattern of numbers is similar to an equilateral triangle.
Examples:
1
2 2 2
3 3 3 3 3
4 4 4 4 4 4 4
5 5 5 5 5 5 5 5 5
C Program to Print Full Pyramid Pattern of Numbers
C
#include <stdio.h>
int main()
{
int rows;
printf ( "Number of rows: " );
scanf ( "%d" , &rows);
for ( int i = 1; i <= rows; i++) {
for ( int j = 1; j <= 2 * (rows - i); j++) {
printf ( " " );
}
for ( int k = 1; k < 2 * i; k++) {
printf ( "%d " , i);
}
printf ( "\n" );
}
return 0;
}
|
Output :
Number of rows: 5
1
2 2 2
3 3 3 3 3
4 4 4 4 4 4 4
5 5 5 5 5 5 5 5 5
4. Full Pyramid of Numbers in 180 Degree
This pattern can be printed by combining half pyramid pattern and an inverted half-pyramid pattern.
Examples:
1
22
333
4444
55555
4444
333
22
1
C Program to Print Full Pyramid of Numbers in 180 Degree
C
#include <stdio.h>
void printPattern( int n)
{
for ( int i = 1; i <= n; i++) {
for ( int j = 1; j <= i; j++)
printf ( "%d" , i);
printf ( "\n" );
}
for ( int i = n - 1; i > 0; i--) {
for ( int j = i; j > 0; j--)
printf ( "%d" , i);
printf ( "\n" );
}
}
int main()
{
int n = 8;
printPattern(n);
return 0;
}
|
Output1
22
333
4444
55555
666666
7777777
88888888
7777777
666666
55555
4444
333
22
1
Time complexity: O(n^2) // For given input n
Auxiliary Space: O(1)
This article is contributed by Sahil Rajput. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.