Software & Finance





C Programming (Turb C++ Compiler) Loops - 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

 

                    *

                   ***

                  *****

                 *******

                *********

               ***********

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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