Java program to print mirrored rhombus star pattern program. We have written the below print/draw mirrored rhombus asterisk/star pattern program in four different ways with sample example and output, check it out. At the end of the program, we added compiler such that you can execute the below codes.
- Print Mirrored Rhombus Star Pattern – Using For Loop
- Print – Using While Loop
- print – Using Do While Loop
1. Using For Loop
1) Read the n value using scanner class, store it in the variable n.
2) Run the outer for loop with the structure for(int i=1;i<=n;i++) to iterate through rows
3) The 1st inner loop prints space from j=n-i to j<n, 2nd inner loop prints charter from j=1 to j=n.
In this example n=6, for i=1, 1st inner loop prints one space for j=5. For j=6 condition fails then comes to next for loop. 2nd loop prints charters from j=1 to j=6 at 1st row. next n=2 ,1st loop prints spaces for j=4,5. 2nd loop prints charters from j=1 to 6 at 2nd row.after 2 iterations this pattern will be printed.
1 2 |
****** ****** |
After all iterations, the given pattern will be printed.
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 |
import java.util.Scanner; public class MRhombusstar { 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=n-i;j<n;j++) { System.out.print(" "); } for(int j=1;j<=n;j++) { System.out.print(c); } System.out.println(); } } } |
Output:
1 2 3 4 5 6 7 8 9 |
Enter N : 6 Enter Symbol : * ****** ****** ****** ****** ****** ****** |
2. Using While Loop
1) While loop is entry checking loop, it checks the condition then only executes the code.
2) Checks the condition at while, while(i<=n), if it is true then j initialized to 1 and 1st inner loop prints space until the condition while(–j>0) is false
3) j initialized to 1,2nd inner loop prints charter until the condition while(j++<=n) is false.
4) Next cursor comes to next line and i value increased by 1, and again starts the execution of outer while loop until (i<=n) 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 31 32 33 34 35 |
import java.util.Scanner; public class MRhombusstar { 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=i; while(--j>0) { System.out.print(" "); } j=1; while(j++<=n) { System.out.print(c); } System.out.println(); i++; } } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
Enter N : 12 Enter Symbol : * ************ ************ ************ ************ ************ ************ ************ ************ ************ ************ ************ ************ |
3. Using Do-While Loop
1) The do-while loop is exit checking loop, it executes the code one time then it checks the condition.
2) For i,j values 1st inner do loop prints one space then checks the code while(–j>0), if this code is true then it prints the space until the condition is false.
3) For j=1, 2nd inner loop prints one charter then checks the condition while(++j<=n), if this condition true it prints charter until the condition is false.
4) Cursor comes to next line and i value increased by 1, then checks the condition at outer do-while loop (i<=n), if this condition is true then the outer do-while loop start the execution again, repeats until the condition i<=n 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 |
import java.util.Scanner; public class MRhombusstar { 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; do { j=i; do { System.out.print(" "); } while(--j>0); j=1; do { System.out.print(c); }while(++j<=n); System.out.println(); i++; } while(i<=n); } } |
Output:
1 2 3 4 5 6 7 8 |
Enter N : 5 Enter Symbol : 5 55555 55555 55555 55555 55555 |