C Exercises

Sep 8, 2020

C program to print full pyramid number pattern

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


Input from user:

Enter the number: 5


Expected output:

        1

      123

    12345

  1234567

123456789



  Step by step logic of the given program:


1. Accept input number from user declare variable say no.


2. Run outer loop from 1 to no which shows rows in pattern:

for(i=1;i<=no;i++)


3. Run inner loop from i to no-1 to print spaces:

for(j=i;j<no;j++)

{

printf(" ");

}


4. Run another inner loop from  1 to (i*2)-1  and inside a loop print value of k:

for(k=1;k<=(i*2)-1;k++)

{

     printf("%d",k);

}


5. For next row print new line   \n ,ouside the block of inner for loop.



  Program to print full pyramid number pattern:


#include<stdio.h>

void main()

{

int no,i,j,k;

/*Accept input*/

printf("Enter the number: ");

scanf("%d",&no);

for(i=1;i<=no;i++)

{

for(j=i;j<no;j++)

{

/*print space*/

printf(" ");

          }

    for(k=1;k<=(i*2)-1;k++)

    {

    /*Print value of k*/ printf("%d",k);

    }

    /*For next row*/ printf("\n");

}

}



Above program shows the following output:


C program to print full pyramid number pattern, star pattern programs in C




No comments:

Post a Comment

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