Java Program for Inverted Hollow Pyramid Star Pattern

Printing Inverted Hollow Pyramid Star Pattern:

Today we will be learning, how to code a Java Program for printing inverted hollow pyramid star pattern. An inverted hollow pyramid star pattern has star only on its boundaries and the rest space is filled by blank spaces.
Go through the page, you can find the code below, along with the algorithm

Java Program for Inverted Hollow Pyramid Star Pattern

Algorithm:

  • Take the number of rows as input from the user and store it in any variable.(‘row‘ in this case).
  • Run a loop ‘row’ number of times to iterate through each of the rows. From i=row to i>0. The loop should be structured as for(int i=row;i>0;i–)
  •  Run a nested loop inside the main loop to print the spaces before the pyramid. From j=1 to j<=row-i. The loop should be structured as for(int j=1;j<=row-i;j++)
  • Inside this nested loop print white space System.out.print(” “); 
  • Run another nested loop inside the main loop after the previous loop to print the stars in each column of a row. From j=1 to j<i*2-1. The loop should be structured as for(int j=1;j<=row-i;j++)
  • Print star under the if condition (i==1 or i==row).
  • Under the else condition  use another if condition to print the rest of the stars.
  • Use if(j==1 || j==i*2-1) to print stars.
  • Else print white space System.out.print(” “);
  • In the main loop print a new line to move to the next line System.out.println();

Code in Java:

import java.util.Scanner;
public class Pattern1 {

	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		System.out.println("Enter Number : ");
		int row=sc.nextInt(); 
		for(int i=row;i>0;i--)
		{
		 for(int j=1;j<=row-i;j++)
		  System.out.print(" ");
		 if(i==1 || i==row)
		 for(int j=1;j<=i*2-1;j++)
		  System.out.print("*");
		else
		 for(int j=1;j<=i*2-1;j++)
		if(j==1 || j==i*2-1)
		  System.out.print("*");
		else
		  System.out.print(" ");
		System.out.println();
     }
  }
}

This code is contributed by Shubham Nigam (Prepinsta Placement Cell Student) 

7 comments on “Java Program for Inverted Hollow Pyramid Star Pattern”


  • SANY

    import java.util.Scanner;

    public class Pattern12 {
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    for (int i = n; i >= 1; i–) {
    for (int s = 1; s <= n – i; s++) {
    System.out.print(" ");
    }
    if (i == n) {
    for (int j = 1; j < 2 * i; j++) {
    System.out.print("*");
    }
    } else if (i == 1) {
    System.out.print("*");
    } else {
    System.out.print("*");
    for (int j = 1; j <= 2 * i – 3; j++) {
    System.out.print(" ");
    }
    System.out.print("*");
    }
    System.out.println(" ");
    }
    }
    }


  • manicampusplacements

    import java.util.*;
    public class Main{
    public static void main(String[] args){
    int n = new Scanner(System.in).nextInt();
    //int m = new Scanner(System.in).nextInt();
    int count = 2*(n) – 1;
    for(int i=1;i<=n;i++){
    for(int j=1;j<i;j++) System.out.print(" ");
    for(int j=1;j<=count;j++){
    if(i==1 || j==1 || j==count) System.out.print("*");
    else System.out.print(" ");
    }
    count -= 2;
    System.out.println();
    }
    }
    }


  • Divya

    import java.util.Scanner;
    public class Main
    {
    public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    int n= sc.nextInt();
    for(int i=1;i<=n;i++){
    for(int k=0;k=0;j–){
    if(i==1||i==n)
    System.out.print(“*”);

    else if(j==(n-i)*2||j==0)
    System.out.print(“0″);
    else System.out.print(” “);
    }
    System.out.println();
    }

    }
    }


  • kuldeepbarod2001

    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();
    }
    }
    }


  • Varsini

    public class Hebeon{
    public static void main(String[] args)
    {
    int num=5;
    for(int row=num;row>=1;row–){
    for(int cols=1;cols=1;col–){

    if(col==(row*2-1)||col==1||row==num)
    System.out.print(“*”);
    else
    System.out.print(” “);
    }
    System.out.println();
    }
    }
    }