Pyramid pattern printing is a very popular logical program and it enhances your logical thinking. In this article, we will learn how to make different types of pyramid patterns in c programming.
Prerequisites:
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 6 types of pyramid patterns here in this article.
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.
Pyramid Example:
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 program to print right half pyramid pattern of star #include <stdio.h> int main()
{ int rows;
printf ( "Number of rows: " );
scanf ( "%d" , &rows);
// first loop for printing rows
for ( int i = 1; i <= rows; i++) {
// second loop for printing similar number in each
// rows
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.
Pyramid Example:
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 program to print inverted half pyramid pattern of number #include <stdio.h> int main()
{ int rows;
printf ( "Number of rows: " );
scanf ( "%d" , &rows);
// first loop for printing rows
for ( int i = rows; i >= 1; i--) {
// second loop for printing similar number in each rows
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.
Pyramid Example:
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 program to print the full pyramid pattern of alphabets #include <stdio.h> int main()
{ int rows;
printf ( "Number of rows: " );
scanf ( "%d" , &rows);
// first loop to print all rows
for ( int i = 1; i <= rows; i++) {
// inner loop 1 to print white spaces
for ( int j = 1; j <= 2 * (rows - i); j++) {
printf ( " " );
}
// inner loop 2 to print numbers
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.
Pyramid Example:
1
22
333
4444
55555
4444
333
22
1
C Program to Print Full Pyramid of Numbers in 180 Degree
// C program to print the pyramid pattern #include <stdio.h> // Print the pattern upto n void printPattern( int n)
{ // Printing upper part
for ( int i = 1; i <= n; i++) {
for ( int j = 1; j <= i; j++)
printf ( "%d" , i);
printf ( "\n" );
}
// printing lower part
for ( int i = n - 1; i > 0; i--) {
for ( int j = i; j > 0; j--)
printf ( "%d" , i);
printf ( "\n" );
}
} // Driver Code int main()
{ int n = 8;
printPattern(n);
return 0;
} // This code is contributed by bhartik021. |
1 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)
5. hollow pyramid of ” * ” & Numbers
This C program print the hollow pyramid means only the border part shows in the pyramid.
Pyramid Example:
*
* *
* *
* *
* * * *
C Program to Print hollow pyramid of ” * ” & Numbers
#include <stdio.h> int main() {
int n = 5;
for ( int i = 1; i <= n; i++) {
// Print leading spaces
for ( int j = i; j < n; j++) {
printf ( " " );
}
// Print asterisks
for ( int k = 1; k <= 2 * i - 1; k++) {
if (k == 1 || k == 2 * i - 1 || i == n) {
printf ( "*" );
} else {
printf ( " " );
}
}
// Move to the next line after each row
printf ( "\n" );
}
return 0;
} |
* * * * * * * *********
6. Diamond Pyramid of ” * ” & Numbers
This C program print the Diamond Pyramid of ” * ” & Number. (for number just replace Stars (*) with their interator (I) in a loop.
*
***
*****
*******
*********
*******
*****
***
*
C program to print Diamond Pyramid of ” * ” & Numbers
#include <stdio.h> int main() {
int n = 5;
// Print the top pyramid
for ( int i = 1; i <= n; i++) {
for ( int j = 1; j <= n - i; j++) {
printf ( " " );
}
for ( int k = 1; k <= 2 * i - 1; k++) {
printf ( "*" );
}
printf ( "\n" );
}
// Print the inverted pyramid
for ( int i = n - 1; i >= 1; i--) {
for ( int j = 1; j <= n - i; j++) {
printf ( " " );
}
for ( int k = 1; k <= 2 * i - 1; k++) {
printf ( "*" );
}
printf ( "\n" );
}
return 0;
} |
* *** ***** ******* ********* ******* ***** *** *
FAQs on “print pyramid in c”
Q1: What is a pyramid pattern in C?
A pyramid pattern is a triangular pattern formed using characters, typically asterisks (*), printed in increasing or decreasing order.
Q2: How do you print a pyramid pattern in C?
To print a pyramid pattern in C, you can use nested for loops.
- The outer loop controls the number of rows.
- The inner loop controls the number of characters to print in each row.
You can use conditional statements to determine the increasing or decreasing pattern.
Q3: What are the different types of pyramid patterns in C?
There are various types of pyramid patterns in C, including:
- Right-angled pyramid
- Inverted pyramid
- Centered pyramid
- Hollow pyramid
- Diamond pyramid