Beginners Programs :

Hollow Diamond Star Pattern Java Program | Patterns

Java program to print hollow diamond star pattern program. We have written below the print/draw hollow 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 so that you can execute the below codes.

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

Using For Loop

1) The first outer for loop will display the half of the hollow diamond pattern, the 2nd outer for loop will display the remaining hollow diamond pattern.

2) The 1st outer for loop executes the set of statements until the condition i<n is false, if the condition is true the 1st inner loop will be executed until the condition is true, then it checks the if condition true it will display character “*”, otherwise else part will display space.

Next 2nd inner loop will be executed until the condition is false. For i=0 the 1st row of the hollow pattern completed. 1st for loop will display this pattern.

3) The 2nd outer for loop executes the statements until the condition is false. The 1st inner loop executes the statements until the condition is false. In this loop if “if” condition is true then it displays character “*” otherwise it displays space.

The 2nd inner loop executes the set of statements until the condition is false, in the 2nd inner loop if “if” condition true it display space else it displays character “*”. The 2nd outer loop will display this pattern.

Output:

Using While Loop

1) 1st outer while loop will execute the code until the condition is true. The 1st inner while loop executes the statements until the condition j<n.

In 1st inner while loop if j<n-i is true it will display character, else it displays space.

After 1st inner loop execution, the 2nd inner loop will execute the statements until the condition j<n is false. In the 2nd inner loop, if the condition is j<i  is true, it will display space else it will display character”*”.

2) The 2nd outer while loop will execute the statements until the condition i<=n is false. The 1st inner loop executes the statements until the condition j<n is false, in this loop if “if” condition is true then displays character “*” otherwise displays space.

After first inner loop execution, the 2nd inner loop will be executed until the condition j<n is false. In this 2nd inner loop if “if” condition is true j<n-i it will display space otherwise it will display character.

Output:

Using Do-While Loop

1) The 1st outer do-while loop will executes the code until the condition i<n is false. The 1st inner loop executes until j<n is false. In 1st inner loop if j<n-i is true it will display character “*” otherwise it will display space.

2) After 1st inner loop execution, the 2nd inner loop will be executed until the condition j<n is false, in this loop if the if condition j<i true it prints space otherwise prints character “*”.

3) The 2nd outer do-while loop will be executed until the condition i<=n. The 1st inner loop execute the statements until the condition is false. In this loop, if condition j<i true then prints character otherwise prints space.

4) After completion of 1st inner loop 2nd inner loop will be executed until the condition j<i is false. The if condition j<n-i true then prints space otherwise prints the character.

Output:

For More:

List of Java Programs – More than 500+ Java Programs

Leave a Reply

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

*