Print Inverted Pyramid Star Pattern

PRINTING PATTERN:

*******

  *****

    ***

      *

PREREQUISITE:

Basic knowledge of C language and use of loops.

ALGORITHM:

  1. Take the number of rows as input from the user and store it in any variable.(‘r‘ in this case).
  2. Run a loop ‘r’ number of times to iterate through each of the rows. From i=r to i>0. The loop should be structured as for( i=r; i>0 : i–).
  3.  Run a nested loop inside the main loop to print the spaces before the pyramid. From k=r to k>i +1. The loop should be structured as for( k=r; k>i+1 ;k–).
  4. Inside this loop print white space.
  5. Run another nested loop after the previous loop to print the stars in each column of a row. From j=0 to j<i*2-1. The loop should be structured as for(j=0 ; j<i*2 ; j++).
  6. Inside this loop print star.
  7. Move to the next line by printing a new line . printf(“\n”)

CODE IN C:

#include<stdio.h>
int main()
{
int i,j,k,r;     //declaring integer variables i,j,k for loops and r for number of rows
printf("Enter the number of rows :\n");     //Asking user for input
scanf("%d",&r);     //saving number of rows in variable r
for(i=r;i>0;i--)     //outer loop for number of rows
   {
      for(k=r;k>i;k--)     //nested loop for spaces before the pyramid
         {
            printf(" ");     //printing white space
         }
      for(j=0;j<i*2-1;j++)     //loop for printing stars
         {
            printf("*");     //printing stars
         }
      printf("\n");     //printing new line after each row
   }
}

TAKING INPUT:
DISPLAYING OUTPUT:

2 comments on “Print Inverted Pyramid Star Pattern”


  • Soumyadeep

    #Python3 code
    n = int(input(‘Enter thenumber of rows:’))
    temp = 1
    for i in range(0,n):
    for j in range(0,i):
    print(‘ ‘,end=”)
    for k in range(0,(2*n-2*i-1)):
    print(‘*’,end=”)
    print()


  • Aninda Ganguly

    {
    int i,j,k,n;
    scanf(“%d”,&n);

    for(i=0;i<n;i++){
    for(k=0;k<i;k++){
    printf(" ");
    }
    for(j=0;j<(n*2)-1-(2*i);j++){
    printf("*");
    }
    printf("\n");
    }