Ex: Write a C program to print half left side of the pyramid star pattern. How to write a C program to print half left side of the pyramid star pattern. C program to print half left side of the pyramid star pattern.
Input from user:
Enter the number:
5
Expected output:
*
**
***
****
*****
Step by step logic of the given program:
1. Accept input number from user declare variable say no.
2. Run outer loop from 1 to no which shows the number of rows in pattern:
for(i=1;i<=no;i++)
3. Run first inner loop from i to no-1 to print spaces:
for(j=i;j<no;j++)
4. Run second inner loop from 1 to i to print star:
for(k=1;k<=i;k++)
5. For next row print \n.
Program:
#include<stdio.h>
void main()
{
int no,i,j,k;
printf("Enter the number:\n");
scanf("%d",&no);
/*Run loops*/
for(i=1;i<=no;i++)
{
for(j=i;j<no;j++)
{
printf(" ");
}
for(k=1;k<=i;k++)
{
printf("*");
}
/*For new row*/
printf("\n");
}
}
Above program shows the following output: