Java program to print mirrored right triangle star pattern program. We have written below the print/draw 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.
- Print Mirrored Right Triangle Star Pattern – Using For Loop
- Print – Using While Loop
- print – Using Do While Loop
Using For Loop
1) Read the n value.
2) Run the outer for loop with the structure for(int i=1;i<=n;i++) to iterate through rows from i=1 to i=n.
3) The condition of outer loop is true then 1st inner loop runs with the structure for(int j=0;j<n-i;j++) and
prints space if i<=n.
4) 2nd inner loop runs with the structure and prints character if j<i.
5) Then cursor comes to next line and 2nd iteration will begin, repeat until i<=n.
In this example n=5, for i=1 i<=n is true, so in 1st inner loop for j=0 j<n-i is true so it prints space,for j=1,2,3 condition true so it prints spaces, in 2nd inner loop for j=0,j<i is true so it prints character,for j=1 condition fails. After 1st iteration —-* will be printed(4 spaces and one character), after all the iterations, this 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 |
import java.util.Scanner; public class MTangle { 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=0;j<n-i;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 |
Enter N : 5 Enter Symbol : * * ** *** **** ***** |
1 2 3 4 5 6 7 8 |
Enter N : 5 Enter Symbol : - - -- --- ---- ----- |
Using While Loop
1) Checks the condition at outer while loop i<=n,if true j initialized to 0.
2) 1st inner while loop prints space if j++<(n-i) is true, repeats until condition fails.
3) j value initialized to 0, 2nd inner while loop prints character j<i is true, and j increased by 1, repeat until the condition is false.
4) Cursor comes to next line, i value increased by 1 and again checks the outer while condition, repeat until the condition is false at outer 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 |
import java.util.Scanner; public class MTangle { 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,j; while(i<=n) { j=0; while(j++<(n-i)) { System.out.print(" "); } j=0; while(j<i) { System.out.print(c);j++; } System.out.println(); i++; } } } |
Output:
1 2 3 4 5 6 7 8 |
Enter N : 5 Enter Symbol : , , ,, ,,, ,,,, ,,,,, |
Using Do-While Loop
1) j initialized to 0. inner do loop prints space once then it checks the condition (++j<n-i) if it is true then it prints space, repeats until the condition is false.
2) Next j initialized to 0, 2nd inner loop prints the character then checks the condition while(j++<i), if true it prints character again checks the condition repeats until the condition fails.
3) Cursor comes to next line, then checks the condition at outer do-while loop while(++i<n) if it is true then outer do loop executes the code, repeats until the condition at outer do while loop 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 MTangle { 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=0,j; do { j=0; do { System.out.print(" "); }while(++j<n-i); j=0; do { System.out.print(c); }while(j++<i); System.out.println(); }while(++i<n); } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
Enter N : 12 Enter Symbol : . . .. ... .... ..... ...... ....... ........ ......... .......... ........... ............ |
More Java Programs: