Java program to print Inverted mirrored right triangle star pattern program. We have written below the print/draw inverted mirrored 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 the scanner object sc and store the value in the variable n.
2) To iterate through rows run the outer for loop with the structure for(int i=n;i>0;i–), to iterate through columns run inner loops.
3) The outer loop executes the inner loops if the condition i>0 is true. The 1st inner loop prints space if the condition j>0, it runs with the structure for(int j=n-i;j>0;j–), the 2nd inner loop prints character if the condition j<i is true,runs with the structure for(int j=0;j<i;j++).
4) In this example n=7, the condition i>0 is true at the outer loop, in the 1st inner loop the condition fails, comes to next inner loop checks the condition. Here if the condition is true then it prints the character, this loop prints the characters from j=0 to j=6, 1st iteration completed, this pattern will be printed *******.
5) for n=6, the 1st inner loop prints one space, the 2nd inner loop prints character from j=0 to j=5. After total iterations of the outer loop, this pattern will be printed.
1 2 3 4 5 6 7 |
******* ****** ***** **** *** ** * |
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 |
import java.util.Scanner; public class HIRtriangle { 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=n-i;j>0;j--) { System.out.print(" "); } for(int j=0;j<i;j++) { System.out.print(c); } System.out.println(); } } } |
Output:
1 2 3 4 5 6 7 8 9 10 |
Enter N : 7 Enter Symbol : * ******* ****** ***** **** *** ** * |
Using While Loop
1) Checks the condition at while i>0 if it is true then j value initialized to j=n-i
a) 1st inner loop prints space if j–>0 is true, this loop repeats until the condition is false. If false, j initialized to 0.
b) 2nd inner loop prints character if j++<i is true, this loop repeats until the condition is false, executes the next statement “System.out.println();
2) Cursor comes to next line i value decreased by value 1 and checks the condition at outer loop. It repeats until the 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 |
import java.util.Scanner; public class HIRtriangle { 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,j; while(i>0) { j=n-i; while(j-->0) { System.out.print(" "); } j=0; while(j++<i) { System.out.print(c); } System.out.println(); i--; } } } |
Output:
1 2 3 4 5 6 7 8 |
Enter N : 5 Enter Symbol : . ..... .... ... .. . |
Using Do-While Loop
1) j value initialized to n-i and while loop prints the space if the condition j–>0 is true. If false j initialized to j=0.
2) Inner do loop prints the character once then it checks the while condition ++j<i, if it is true then it prints character, it repeats until the condition while(++j<i); is false, next cursor comes to next line.
3) Then it checks the condition while(–i>0) at outer do loop, if it is true then do loop executes the inner code, it repeats until the 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 |
import java.util.Scanner; public class HIRtriangle { 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,j; do { j=n-i; while(j-->0) { System.out.print(" "); } j=0; do { System.out.print(c); }while(++j<i); System.out.println(); } while(--i>0) ; } } |
Output:
1 2 3 4 5 6 7 8 |
Enter N : 5 Enter Symbol : * ***** **** *** ** * |
For More Java Programs Check Here – More than 500+ Java Programs are Published Here