Beginners Programs :

Java Half Diamond Star Pattern Program | Patterns

Java program to print Half Diamond star pattern program. We have written below the print/draw Half Diamond 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.

  • Using For Loop
  • Using While Loop
  • Using Do While Loop

Using For Loop

1) The for is useful to execute a set of statements n.no of times.

2) The outer loops iterate through rows and inner loops iterate through columns.

3) The first outer for loop will prints the half of the half diamond pattern, 2nd outer for loop will print the remaining half of the half diamond pattern.

4) 1st outer for loop iterates until the condition i<=n is false, the inner loop will display the character until the condition j<i. The 1st outer loop will print this pattern.

Then cursor goes to next line. 2nd outer for loop will be executed.

5) 2nd outer for loop iterates until the condition i>0 is false, the inner loop will display character until j<=i, 2nd outer for loop will print this pattern.

Output:

Using While Loop

1) Until i<=n is false the 1st outer while will be executed, the inner loop will display the character until the condition j++<=i is false. 1st outer while loop displays the half pattern of half diamond.

After the 1st outer while loop execution, the cursor goes to next line. Then 2nd outer loop execution started.

2) Until the condition i>0 is false the 2nd outer while loop will be executed. The inner loop will display the character until the condition j++<=i is false. The 2nd outer loop will display the remaining half pattern of half diamond.

Output:

Using Do-While Loop

1) The 1st outer do-while loop is executed until the condition ++i<=n is false, the inner loop will display the character until the condition++j<=i is false. The 1st outer do-while loop will display the half of the pattern of half diamond.

2) Then cursor come to next line and 2nd outer do-while will be executed until the condition–i>0 is false. The inner loop will display the character until the condition ++j<=i is false. The 2nd outer do-while loop will display the remaining half pattern of half diamond.

Output:

Leave a Reply

Your email address will not be published. Required fields are marked *

*