Java program to print Rhombus star pattern program. We have written the below print/draw Rhombus star pattern program in four different ways with sample example and output, do check it out. At the end of the program, we have added the compiler so that you can execute the below codes.
- Print Rhombus star pattern program – Using For Loop
- Print – Using While Loop
- Print – Using Do While Loop
Using For Loop
1) Read n value using scanner object and 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, run the inner loops to iterate through columns.
3) Condition at outer loop is true then 1st inner loop runs with the structure for(int j=1;j<=n-i;j++) and prints space if j<=n is true.2nd inner loop runs with the structure for(int j=1;j<=n;j++) and prints charter if j<=n is true. Repeats until condition at outer loop is false.
4) In this example n=5,for i=1 i<=n is true,so 1st inner loop prints 4 spaces from j=1 to j=4, after that 2nd inner loop prints 5 charters from j=1 to j=5 .i.e in 1st row 4 spaces and 5 charters will be printed. For i=2, the for loop prints 3 spaces for j=1,2,3,2nd inner loop prints 5 charters.After total iteration, 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 26 27 28 29 30 31 32 33 34 35 36 |
import java.util.Scanner; public class Rhombusstar { 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(" "); } for(int j=1;j<=n;j++) { System.out.print(c); } System.out.println(); } } } |
Output:
1 2 3 4 5 6 7 8 |
Enter N : 5 Enter Symbol : * ***** ***** ***** ***** ***** |
Using While Loop
1) While loop is entry checking loop, it checks the condition then only executes the code.
2) Check the condition at outer while, for, given i value, if it is true then j=1, 1st inner loop prints space until the condition j++<=n-i is false.
3) j value initialized to 1 and 2nd while loop prints charter until the condition j++<=n is false.
4) Cursor comes to next line and i value increased by 1.
5) Repeats until the condition at outer while 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 |
import java.util.Scanner; public class Rhombusstar { 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; while(i<=n) { int j=1; while(j++<=n-i) { 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 |
Enter N : 6 Enter Symbol : * ****** ****** ****** ****** ****** ****** |
Using Do-While Loop
1) The do-while loop is exit checking loop, it executes the code once and then checks the condition.
2) For the given values of i,j the outer loop starts the execution, the 1st inner loop prints one space and then checks the condition while(j++<=n-i). If this condition is true, it prints space until the condition is false.
3) j initialized to 1st and 2nd inner loop prints one character and then checks the condition while(++j<=n), if this condition is true, it prints the character until the condition is false.
4) Cursor comes to next line and i value increased then checks the condition at outer do-while loop while(i<=n), if this condition is true then next iteration will start, repeats until condition at outer do-while loop while(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 Rhombusstar { 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; do { int j=1; do { System.out.print(" "); } while(j++<=n-i); 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 |
More Programs: