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

3 . Program to print half inverted pyramid of stars in C :

Solutions :

/******************************************************************************

Program to print inverted half pyramid in C  using Stars
https://bptutorials.com

*******************************************************************************/
#include<stdio.h>

int main()
{
    int row, n, col;

    printf("Enter number of Rows: ");
    scanf("%d", &n);

    // this loop for rows 
    
    for(row=n;row>=1;row--)
    {
         // this loop for columns 
         
        for(col=1;col<=row;col++)
        {
            printf("*"); // print stars
        }
        printf("\n");
    }

    return 0;
}

Output :

Program to print inverted half pyramid in C  using Stars

4 . Program in C to print Diamond pattern full star :

Solutions :

/******************************************************************************
 Program in C to print Diamond pattern full star 
 By https://bptutorials.com

*******************************************************************************/
   
   #include<stdio.h>
   #include<conio.h>
   int main()
   {
    int n, space, row, star;
    printf("Enter number of rows: ");
    scanf("%d",&n);
    
     //  print uper triangle
     for(row = 0; row <= n; row++)
     {
     for(space = n; space > row; space--)
     printf(" ");
     for(star=0; star<row; star++)
     printf("* ");
     printf("\n");
     }
     
     // print lower triangle
     for(row = 1; row < n; row++)
     {
     for(space = 0; space < row; space++)
     printf(" ");
     for(star = n; star > row; star--)
     printf("* ");
     //  move to the next row after print
     printf("\n");
     }
     return 0;
   }

Output :

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

5 . Write a program in C to print full hollow pyramid using * :

Solutions :

/*********************************************************
 * Write a C Program to print full hollow pyramid using * 
 * 
 *  https://bptutorials.com
*********************************************************/

#include<stdio.h> 

int main() 
{
    int n, row, space, col ;

    printf("Enter number of rows: ");
    scanf("%d", &n);

    printf("\n");

    // loop for rows
    for(row = 1; row <= n; row++)
    {   
        // loop to print leading spaces in each row
        for( space = 0; space <= n - row; space++)
        {
            printf("   ");
        }

        // this loop for print *
        for(col = 1; col <= row * 2 - 1; col++)
        {

            if (col == 1 || (col == row * 2 - 1) || row == n )
            {
                printf(" * ");
            }
            else
            {
                printf("   ");            
            }            
        }               

        printf("\n");
    }

    return 0;
}

Output :

Write a C Program to print full hollow pyramid using *

6.Write a C Program to print Inverted full hollow pyramid using * :

Solution :

/*********************************************************
 * Write a C Program to print Inverted full hollow pyramid using * 
 * 
 *  https://bptutorials.com
*********************************************************/

#include<stdio.h> 

int main() 
{
    int n, row, space, col ;

    printf("Enter number of rows: ");
    scanf("%d", &n);

    printf("\n");

    // loop for rows
    for(row = n; row >= 1; row--)
    {   
        // loop to print leading spaces in each row
        for( space = n-row; space >=1; space--)
        {
            printf("   ");
        }

        // this loop for print *
        for(col = row*2-1; col >= 1; col--)
        {

            if (col == 1 || (col == row * 2 - 1) || row == n )
            {
                printf(" * ");
            }
            else
            {
                printf("   ");            
            }            
        }               

        printf("\n");
    }

    return 0;
}

Output :

Write a C Program to print Inverted full hollow pyramid using *
Share This!

Leave a Reply

Your email address will not be published.