Latest :

Rhombus Star Pattern Program In Java – Patterns

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.

Output:

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.

Output:

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.

Output:

More Programs:

x

Check Also

Prime Number Java Program – 1 to 100 & 1 to N | Programs

Prime Number Java Program –  Java Program to Check Whether a Number is Prime or ...