Sep 6, 2020

C program to print number pyramid pattern

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


Input from user:

Enter the number: 5


Expected output:

             1

           1  2

         1  2  3

       1  2  3  4

     1  2  3  4  5



  Step by step logic of the given program:


This pattern is same as previous exercises star pyramid pattern:

       *

      **

     ***

    ****

   *****


Difference is:

In this pattern we only need to replace the * with value of .


1. Accept input number from user.

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

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

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

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

{

printf(" ");

}

4. Run another inner for loop from 1 to i which shows columns in pattern:

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

5. Inside the second inner for loop print value of k with one space:

printf("%d ",k);

6. After that outside the block of  inner for loop print next line \n for print new row.



  Program to print number pyramid pattern :


#include<stdio.h>
void main()

{

	int no,i,j,k;

	printf("Enter the number:");

	scanf("%d",&no);

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

	{

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

		{

		/*Print spaces*/

		printf(" ");

      		}

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

		{

		/*print value of k with one space...to print it like a pyramid*/

		printf("%d ",k);

		}

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

	}


}




Above program shows the following output:


C program to print number pyramid pattern, number pyramid pattern, number patterns in C



No comments:

Post a Comment

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