Java program to print Hollow Inverted Pyramid star pattern program. We have written below the print/draw Hollow Inverted Pyramid asterisk/star pattern program in four different ways with sample example and output, check it out. At the end of the program, we have added compiler so that you can execute the below codes.
- Using For Loop
- Using While Loop
- Using Do While Loop
Using For Loop
1) Read the entered value and store in the variable n.
2) Run the outer loop with the structure for(int i=n;i>0;i–) for n times.
3) Run the 1st inner loop with the structure for(int j=1;j<=n-i;j++)
prints space for i>0 and j<=n-i
4) If the first inner loop condition is false then checks the” if” condition
– condition true 1st loop prints character for ( i==1 or i==n) and j<=i*2-1,
-condition false 2nd loop prints character for (j==1 || j==i*2-1), prints space for (j!=1&j!=i*2-1) until j<=i*2-1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
import java.util.Scanner; public class HItangle { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter N : "); int n=sc.nextInt(); System.out.print("Enter Symbol : "); char c = sc.next().charAt(0); for(int i=n;i>0;i--) { for(int j=1;j<=n-i;j++) { System.out.print(" "); } if(i==1 || i==n) for(int j=1;j<=i*2-1;j++) { System.out.print(c); } else { for(int j=1;j<=i*2-1;j++) { if(j==1 || j==i*2-1) System.out.print(c); else System.out.print(" "); } } System.out.println(); } } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Enter N : 10 Enter Symbol : * ******************* * * * * * * * * * * * * * * * * * |
Using While Loop
1) The outer while will iterate n times,if the condition is true at outer loop then inner while loop condition true then prints space. condition false check the condition at “if”.
2)
a)If condition true first while loop prints character until j++<=i*2-1 is false.
b) condition false next while loop prints character for j==1 , j==(i*2)-1), until while(j<=(i*2)-1) condition is false.
c) prints space for j!=1 and J!=(i*2)-1, until while(j<=(i*2)-1) condition is false.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
import java.util.Scanner; public class HItangle { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter N : "); int n=sc.nextInt(); System.out.print("Enter Symbol : "); char c = sc.next().charAt(0); int i=n; int j; while(i>0) { j=1; while(j++<=n-i) { System.out.print(" "); } if(i==1 || i==n) { j=1; while(j++<=i*2-1) { System.out.print(c); } } else { j=1; while(j<=(i*2)-1) { if(j==1 || j==(i*2)-1) System.out.print(c); else System.out.print(" "); j++; } } System.out.println(); i--; } } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Enter N : 10 Enter Symbol : + +++++++++++++++++++ + + + + + + + + + + + + + + + + + |
Using Do-While Loop
1) Outer do-while loop iterates until i>0 condition if false.
2) The inner loop prints space, after that it checks the condition, it prints space until the condition (++j <=n-i+1); is false.
3) “if” condition true 1st do-while loop prints character until the condition (++j <=i*2-1) is false.
4) “if” condition false 2nd do-while loop prints character if (j==1 || j==i*2-1) is false, until the condition (++j <=i*2-1) is false.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
import java.util.Scanner; public class HItangle { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter N : "); int n=sc.nextInt(); System.out.print("Enter Symbol : "); char c = sc.next().charAt(0); int i=n; int j; do { j=1; do { System.out.print(" "); }while(++j <=n-i+1); if(i==1 || i==n) { j=1; do { System.out.print(c); }while(++j <=i*2-1); } else { j=1; do { if(j==1 || j==i*2-1) System.out.print(c); else System.out.print(" "); }while(++j<=i*2-1); } System.out.println(); --i; } while(i>0); } } |
Output:
1 2 3 4 5 6 7 8 9 10 |
Enter N : 7 Enter Symbol : * ************* * * * * * * * * * * * |