Write a program to print pyramid pattern in C like below pattern

pyramid pattern in C

Solution :

To print above pyramid pattern we need 3 loop. First loop for rows and Second loop for print spaces and third loop for print star in each row.

#include <stdio.h>
#include <stdlib.h>

int main()
{
  int rows, colums, num;

  printf("Enter numbers of rows in pyramid \n");
  scanf("%d", &num);  // assign user enter number to num

  for (rows = 1; rows <= num; rows++)  // Loop for rows
  {
    for (colums = 1; colums <= num-rows; colums++)  // Loop to print spaces in a row
      printf(" ");

    for (colums = 1; colums <= 2*rows - 1; colums++) // Loop to print stars in a row
      printf("*");

    printf("\n");
  }

  return 0;
}

2.Write a program in C to print Hollow Mirrored Right Triangle Star Pattern

Solution :

Logic -1

#include <stdio.h>

int main()
{
   int i,j,n;
   
   // get user input
   printf("Enter the number of rows:");
   
   // store user input to variable n
   scanf("%d",&n);
   // loop for rows
       for(i=1;i<=n;i++)
       {
           // loop for column
           for(j=1;j<=n;j++)
           {
               // main logic   
               if(j>=(n+1)-i)
               
                printf("*");
                
                else
                
                printf(" ");
           }
            printf("\n");
               
           
       }
 }

Output :

Write a program  to print  pyramid pattern in C like below pattern 1

Logic – 2

#include <stdio.h>

int main()
{
    int row, col, k, n;
    
    printf("Enter no. of rows : ");
    scanf("%d", &n);
    for(row=n; row>1; row--)
    {
        for(col=1; col<row; col++)
        {
            printf(" ");
        }
        for(k=n; k>=row; k--)
        {
            printf("*");
        }
        printf("\n");
    }
    return 0;
}

Output :

Write a program  to print  pyramid pattern in C like below pattern 2
Share This!

Leave a Reply

Your email address will not be published. Required fields are marked *