Creating Pyramid

Creating Pyramid using loops



I have given here a simple program withg loops to build a pyramid with *. You can replace * with any other character you like.

Source Code


#include <stdio.h>

int main()
{
    int i,j,k, n;
    printf("Enter number of rows for pyramid: ");
    scanf("%d", &n);
 
    printf("\n");
 
    for(i = 1; i <= n; i++)
    {
        for(j = 0; j < (n - i); j++)
            printf(" ");
        for(j = 1; j <= i; j++)
            printf("*");
        for(k = 1; k < i; k++)
            printf("*");
        printf("\n");
    }
    printf("\n\n");
 
    return 0;
}

Output


Enter number of rows pyramid: 21

                    *
                   ***
                  *****
                 *******
                *********
               ***********
              *************
             ***************
            *****************
           *******************
          *********************
         ***********************
        *************************
       ***************************
      *****************************
     *******************************
    *********************************
   ***********************************
  *************************************
 ***************************************
*****************************************

২টি মন্তব্য:

  1. #include
    #include
    main()
    {
    int a,b,row,temp=0;
    printf("Enter the number of rows in pyramid: ");
    scanf("%d",&a);
    temp=a;
    for(row=1;row<=a;row++)
    {
    for(b=1;b<temp;b++)
    printf(" ");
    temp--;
    for(b=1;b<=2*row-1;b++)
    printf("*");
    printf("\n");
    }
    return 0;
    }

    উত্তরমুছুন
  2. #include
    main()
    {
    int row,c,n,temp;
    printf("Enter the number of rows:");
    scanf("%d",&n);
    temp=n;
    for(row=1; row<=n; row++)
    {
    for(c=1; c<temp; c++)
    printf("*");
    temp--;
    for(c=1; c<=2*row-1; c++)
    printf("*");
    printf("\n");
    }
    return 0;
    }

    উত্তরমুছুন