Print Hollow 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 nested loop print white space.
  5. Run another nested loop inside the main 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-1; j++).
  6. Print star under the if condition (i==r).
  7. Under the else condition  use another if condition to print the rest of the stars.
  8. Use if(j==0 || j==i*2-2) to print stars.
  9. Else print white space
  10. In the main loop print a new line to move to the next line.

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
         {
            if(i==r)      //condition to print the first row
               {
                  printf("*");     //printing stars in the first row
               }
            else     //else condition for printing the rest of the pyramid
               {
                  if(j==0||j==i*2-2)     //if condition to print the starting and ending stars in a row
                     {
                        printf("*");      //printing stars
                     }
                  else      //else condition for printing white space
                     {
                        printf(" ");     //printing white space
                     }
                }
          }
      printf("\n");     //printing newline after each row
   }
}

TAKING INPUT:

DISPLAYING OUTPUT:

8 comments on “Print Hollow Inverted Pyramid Star Pattern”


  • kuldeepbarod2001

    ———————–JAVA CODE –BY KULDEEP BAROD—————————————
    public class Demo {
    public static void main(String[] args) {
    int n = 4;
    for (int i = 1; i <= n; i++) {
    for (int k = 1; k <= i; k++) {
    System.out.print(" ");
    }
    for (int j = 1; j <= ((n – i) * 2) + 1; j++) {
    if (i == 1 || j == 1 || j == ((n – i) * 2) + 1) {
    System.out.print("*");
    } else {
    System.out.print(" ");
    }
    }
    System.out.println();
    }
    }
    }


  • Vijaykhede

    int i,j,k;
    for(i=4;i>=1;i–){
    for(k=4-i;k>=1;k–){
    System.out.print(” “);
    }
    for(j=i*2-1;j>=1;j–){
    if(j==i*2-1||i==1||i==4||j==1)
    System.out.print(“*”);
    else
    System.out.print(” “);
    }
    System.out.println();
    }


  • Arthi

    #python
    n = int(input())
    for i in range(n):
    if i==0 or i==n-1:
    print(‘ ‘*i+’*’*(2*n-2*i-1))
    else:
    print(‘ ‘*i+’*’+’ ‘*(2*n-2*i-3)+’*’)


  • Prithuraj

    n = int(input(“Enter the number of rows “))
    for rows in range(n, 0, -1):
    for col in range(0, n * 2 – 1):
    if rows == n or rows + col == 4 or col – rows == 2:
    print(“*”, end=””)
    else:
    print(” “, end=””)
    print()


  • Mo

    C++ EASY SOLOTION
    ADIL KHAN
    #include

    using namespace std;

    int main()
    {
    int n;
    cin>>n;
    for(int i=n;i>=1;i–) //using reverse iteration
    {
    for(int k=n;k>i;k–)//To print space
    cout<<" ";
    for(int j=1;j<=2*i-1;j++)
    {
    if(i==n||j==1||j==2*i-1)
    cout<<"*";
    else
    cout<<" ";
    }
    cout<<endl;
    }

    return 0;
    }


  • Neerajsingh

    ////////////////////////// By ——@ neeraj singh chowhan—————————————–////////////////////////
    n=9
    a=0
    for i in range(n,0,-1):
    if(i==n):
    print(‘*’*(i*2-1))
    elif(i==1):
    print((” “*a)+’*’+(” “*a))
    else:
    print((” “*a)+’*’+(” “*(((i*2)-1)-2))+”*”+(” “*a))
    a=a+1

    ///////////////————————python code for it —————————/////////////////////////////////////////////////////


  • Chandrachur

    import java.util.Scanner;

    public class inverted_hollow_pyramid {
    public static void main(String args[])
    {
    Scanner sc=new Scanner((System.in));
    int n=sc.nextInt();
    for(int i=0;i<n;i++)
    {
    for(int j=0;j=i*2;j–)
    {
    if(i==0||j==n+2||j==i*2||i==n-1)
    System.out.print(“*”);
    else
    System.out.print(” “);
    }
    System.out.println();
    }
    }
    }


  • Chandrachur

    Java code:
    import java.util.Scanner;

    public class inverted_hollow_pyramid {
    public static void main(String args[])
    {
    Scanner sc=new Scanner((System.in));
    int n=sc.nextInt();
    for(int i=0;i<n;i++)
    {
    for(int j=0;j=i*2;j–)
    {
    if(i==0||j==n+2||j==i*2||i==n-1)
    System.out.print(“*”);
    else
    System.out.print(” “);
    }
    System.out.println();
    }
    }
    }