Java program to print Hollow mirrored right triangle star pattern program. We have written below the print/draw hollow 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) Scanner class method nextInt() reads the entered value, initialized to the variable n.
2) Run the outer for loop to iterate through rows with the structure for(int i=1;i<=n;i++), run the inner loops to iterate through columns.
3) Checks the condition at outer loop,if true then a) 1st inner for loop runs with the structure for(int j=1;j<=n-i;j++) and prints space if the condition is true.
4) If if condition true
a) 1st inner for loop under if condition, iterates with the structure
for(int j=1;j<=i;j++) and prints character if j<=i is true.
b) If “if” condition is false then 2nd inner loop iterates with the structure
for(int j=1;j<=i;j++) and prints character for j=1 or j=i, prints space for j!=1 and j!=i.
5) Cursor comes to next line and outer loop executes the code, repeats until i<=n is false.
6) In this example n value is 5, for i=1 condition at outer loop i<=n is true, condition at inner loop j<=n-i is true, so from j=1 to j=4 at 1st row 4 spaces will be printed. “If” condition is true for i=1, so inner loop prints one character at 1st row, then cursor comes to next line outer loop iterate with i=2, condition at outer loop is true, condition at inner loop is true, so inner loop prints 3 spaces from j=1 to j=3 at 2nd row. “if” condition is false so comes to else part, the for loop prints 2 characters for j=1 and j=2. After completion of all iterations, the following pattern will be printed.
1 2 3 4 5 |
. .. . . . . ..... |
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 |
import java.util.Scanner; public class MHtangle { 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=1;i<=n;i++) { for(int j=1;j<=n-i;j++) { System.out.print(" "); } 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(); } } } |
1 2 3 4 5 6 7 8 |
Enter N : 5 Enter Symbol : . . .. . . . . ..... |
Using While Loop
1) i value initialized to 1, checks the condition at while i<=n, the condition is true then j initialized to 1.
2) The 1st inner loop prints space until j<=n-i is false.
3) If “if” condition is true
a) 1st inner loop then j=1 and while loop prints character until the condition j<=i is false.
b) condition is false, then while loop prints character until j<=i if the condition if(j==1 || j==i) is true, otherwise it prints space.
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 |
import java.util.Scanner; public class MHtangle { 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=1; int j; while(i<=n) { j=1; while(j<=n-i) { System.out.print(" "); j++; } 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 |
Enter N : 5 Enter Symbol : * * ** * * * * ***** |
3. Using Do-While Loop
1) j=1, inner do loop prints one space then checks the condition while(++j<=(n-i+1)), if it is true then prints space and checks the condition repeats until the condition is false.
2) If “if condition”
a) true, inner do loop prints one character then checks the condition while(++j <=i); repeats until condition is false.
b) false, comes to else part and checks the if condition, it is true for j=1, so prints one character, then checks the while condition (++j<=i), this loop prints character for j=1 or j=i, it prints space other than these j values.
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 |
import java.util.Scanner; public class MHtangle { 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=1; 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); } else { j=1; do { if(j==1 || j==i) System.out.print(c); else System.out.print(" "); }while(++j<=i); } System.out.println(); ++i; } while(i<=n); } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 |
Enter N : 9 Enter Symbol : * * ** * * * * * * * * * * * * ********* |
More Programs: