In this article, we are going to learn how to print a half Pyramid using the C language. We will try to use two different techniques to print this pattern.
- Using normal for loop to print Pyramid pattern.
- Using decrement Operator in for loop to print Pyramid pattern.
- Using normal for loop to print Pyramid pattern.
In this example, we will use a simple for loop to print the half Pyramid.
*
* *
* * *
* * * *
* * * * *
1. C Programs to print Half start Pyramid
In this c program we are using the for loop to print a half pyramid pattern in C
#include<stdio.h>
int main()
{
int num;
printf("Please enter the size for Pyramid: ");
scanf("%d",&num);
for(int row=1;row<=num;row++)
{
for(int col=1;col<=row;col++)
{
printf("* ");
}
printf("\n");
}
return 0;
}
Actual Output:
Please enter the size for Pyramid: 5
*
* *
* * *
* * * *
* * * * *
2. C Programs to print Half start Pyramid decrement operator
In this example, we are using the for loop with a decrement operator for the Pyramid pattern print.
#include<stdio.h>
int main()
{
int num;
printf("Please enter the size for Pyramid: ");
scanf("%d",&num);
for(int i=num; i>=1; i--)
{
for(int j=i;j<=num;j++)
{
printf("* ");
}
printf("\n");
}
return 0;
}
Output:
Please enter the size for Pyramid: 5
*
* *
* * *
* * * *
* * * * *
3. C Programs to print Half start Pyramid using Nested loop
In this example, we are using the for loop with a decrement operator in the inner for loop to print the Pyramid pattern.
#include<stdio.h>
int main()
{
for(int i=1;i<=5;i++)
{
for(int j=i;j>=1;j--)
{
printf("* ");
}
printf("\n");
}
return 0;
}
Output:
Please enter the size for Pyramid: 5
*
* *
* * *
* * * *
* * * * *