Java program to print hollow inverted right triangle star pattern program. We have written below the print/draw hollow inverted right triangle 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 n value using scanner class method as sc.nextInt().
2) Run the outer for loop with the structure for(int i=n;i>0;i–) to iterate through rows, to iterate through columns run the inner for loops.
3) The 1st for loop prints character if the condition if(i==1 || i==n) is true and j<=i is true, with the structure
for(int j=1;j<=i;j++).
After completion of all iterations, the following pattern will be printed.
1 2 3 4 5 6 7 8 9 |
000000000 0 0 0 0 0 0 0 0 0 0 0 0 00 0 |
Using For Loop:
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 |
import java.util.Scanner; public class HItriangle { 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--) { if(i==1 || i==n) for(int j=1;j<=i;j++) { System.out.print(c); } else { for(int j=1;j<=i;j++) { if(j==1 || j==i) System.out.print(c); else System.out.print(" "); } } System.out.println(); } } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 |
Enter N : 9 Enter Symbol : 0 000000000 0 0 0 0 0 0 0 0 0 0 0 0 00 0 |
Using While Loop
1) Checks the condition at while i>0, if true it executes the inner statements.
2) checks the if condition if(i==1 || i==n)
a) if true j initialized to 1, executes the 1st inner while loop, this loop prints character if the condition while(j <=i) is true, then increases the j value by 1, repeats until the condition is false.
b) if false j initialized to 1, while loop prints character if ( if(j==1 || j==i) true and (j<=i)) otherwise prints space, then j value increased by 1, repeats until j<=i is false.
3) Cursor comes to next line i value decreased by 1 and condition at outer while will be checked, repeats until i>0 is false at outer while loop.
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 HItriangle { 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) { if(i==1 || i==n) { j=1; while(j <=i) { System.out.print(c); j++; } } else { j=1; while(j<=i) { if(j==1 || j==i) System.out.print(c); else System.out.print(" "); j++; } } System.out.println(); i--; } } } |
Output:
1 2 3 4 5 6 7 8 9 |
Enter N : 6 Enter Symbol : . ...... . . . . . . .. . |
Using Do While Loop
1) In outer do loop checks the if condition
a)if true 1st inner do loop will execute, j initialized to 1 and prints the character once then checks the condition at while(++j <=i), if it is true then prints the character and then checks the condition, repeats until while(++j <=i) condition is false.
b) if false, j initialized to 1, the do loop prints the character if the condition if(j==1 || j==i) is true else prints space, then checks the condition at while(++j<=i), repeats until while(++j<=i) condition is false.
2) Cursor comes to next line, then checks the condition at outer do-while loop while(–i>0), repeat until this 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 HItriangle { 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 { if(i==1 || i==n) { j=1; do { System.out.print(c); }while(++j <=i); } else { j=1; do { if(j==1 || j==i) System.out.print(c); else System.out.print(" "); }while(++j<=i); } System.out.println(); } while(--i>0); } } |
Output:
1 2 3 4 5 6 7 8 9 |
Enter N : 6 Enter Symbol : * ****** * * * * * * ** * |
More Java Programs: