C program to print inverted pyramid number pattern

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


Input from user:

Enter the number: 5


Expected output:

      1  2  3  4  5

        1  2   3  4

          1   2  3

             1  2

               1




  Step by step logic of the given program:


1. Accept input(number) from user which shows number of rows, declare variable say no.

2. Run one outer  loop from 1 to no:
  for(i=1;i<=no;i++)

3. To print spaces run another for loop from 1 to i-1:
for(j=1;j<i;j++)
  printf(" ");
    

4. To print value of k use another one inner for loop from 1 to (no+1)-i:
for(k=1;k<=(no+1)-i;k++)
  {
  printf("%d ",k);
  }
    

5. For new row, move to next line using \n.





  Program to print inverted pyramid number pattern:


#include<stdio.h>
void main()
{
 int no,i,j,k;
 
 printf("Enter the no:\n");
 scanf("%d",&no);
 /*Run outer loop from 1 to no*/
 for(i=1;i<=no;i++)
 {   
   /*Run inner loop from 1 to i-1 to print spaces*/
  for(j=1;j<i;j++)
  {
  printf(" ");
  }
  /*Run another inner loop from 1 to (no+1)-i*/
  for(k=1;k<=(no+1)-i;k++)
  {
   /*print value of k with one space*/
  printf("%d ",k);
  }
  /*For next row*/
 printf("\n");
 }

}


Above program shows the following output:

C program to print inverted pyramid number pattern



No comments:

Post a Comment

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