C Program To Print Pyramid Star Pattern

Ex: Write a C program to print  pyramid star pattern. How to write a C program to print  pyramid star pattern. C program to print  pyramid star pattern.


Input from user:
Enter the number:
7

Expected output:
       *
      **
     ***
    ****
   *****
  ******
 *******



  Step by step logic of the given program:


1. Accept input (number) from user which shows size of the pyramid, declare variable say no.

2. Run one outer for loop from 1 to no:
  for(i=1;i<=no;i++)

3. To print spaces run another for loop from i to no-1:
for(j=i;j<no;j++)
    {
    printf(" ");
    }

4. To print star run another one inner for loop from 1 to i:
for(k=1;k<=i;k++)
    {
    printf(" *");
    }

5. For new row, move to next line using \n.




Program:

#include<stdio.h>
void main()
{
  int i,j,k,no;

  printf("Enter the number:\n");
  scanf("%d",&no);

  for(i=1;i<=no;i++)
  {
  /*Print spaces*/
    for(j=i;j<no;j++)
    {
  printf(" ");
    }
    /*print star*/
    for(k=1;k<=i;k++)
    {
    printf(" *");
    }
    /*Move to the next line*/
  printf("\n");
  }
  
}



Above Program's show's the following output:

C Program to pyramid star pattern, star pattern in C


No comments:

Post a Comment

If you have any doubts, please discuss here...👇