Program for Pyramid Pattern
Pyramid Pattern Program
We will write a C program to print pyramid pattern and we can print half pyramid, full pyramid, inverted pyramid etc. This will help to understand the basic structure of programming. In this program , we will print the pattern of half pyramid, full pyramid and inverted pyramid by using proper syntax and algorithms.
Working of Program :
In the program, we will require some inputs from the user to print the pyramid pattern.
Syntax for For() Loop:
for(initialization,condition,increment/decrement)
{
// Statements
}
Problem 1
Write a program to print the half pyramid pattern using for loop.
- Firstly, we have to enter the input.
- Then print that half pyramid pattern.
Code
#include<stdio.h>
int main ()
{
int i, j, rows;
printf (" Enter a number of rows = ");
scanf ("%d", &rows);
printf ("\n");
for (i = 1; i <= rows; ++i)
{
for (j = 1; j <= i; ++j)
{
printf ("* ");
}
printf ("\n");
}
return 0;
}
Output
Enter a number of rows = 6 * * * * * * * * * * * * * * * * * * * * *
Note:
In the following program we will write a program to print the half pyramid pattern using for loop.
Problem 2
Write a program to print the inverted half pyramid pattern using for loop.
- Firstly, we have to enter the input.
- Then print that inverted half pyramid pattern.
Code
#include<stdio.h>
int main()
{
int i, j, rows;
printf (" Enter the number rows = ");
scanf("%d", &rows);
printf("\n");
for (i = rows; i > 0; i--)
{
for (j = i; j > 0; j--)
{
printf ("* ");
}
printf ("\n");
}
return 0;
}
Output
Enter the number rows = 7 * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Problem 3
Write a program to print the inverted right half pyramid pattern using for loop.
- Firstly, we have to enter the input.
- Then print that inverted right half pyramid pattern.
Code
#include<stdio.h>
int main()
{
int i, j, rows, k;
printf (" Enter the number rows = ");
scanf("%d", &rows);
printf("\n");
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= i; j++)
{
printf(" ");
}
for (k = i; k <= rows; k++)
{
printf("*");
}
printf ("\n");
}
return 0;
}
Output
Enter the number rows = 9
*********
********
*******
******
*****
****
***
**
*
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Login/Signup to comment