Latest :

Java Program To Print Diamond Star Pattern | Programs

Java program to print diamond star pattern program. We have written the below print/draw diamond asterisk/star pattern program in four different ways with sample example and output do check it out. At the end of the program, we added compiler such that you can execute the below codes – Also check Number pattern Programs in Java.

  • Print Diamond star pattern program – Using For Loop
  • Print – Using While Loop
  • print – Using Do While Loop

1. Using For Loop – Diamond Star / Asterisk Pattern Code

The explanation for the above code:

1) For loop is useful when the set of statements need to execute N no. at times.

2) First outer for loop displays half of the diamond pattern, 2nd outer for loop displays the remaining half of the pattern.

3) First outer for loop executes the code until the condition false if the condition at for loop true it checks the 1st inner loop condition if true its display space, if false it comes to 2nd inner loop and checks the condition,  if true it display charter “*” / Asterisk

3) cursor comes to next line, 2nd outer for loop will execute the code until the condition false. In 2nd outer for loop if the condition true then it checks condition at 1st inner loop if condition true then it displays space otherwise it goes to 2nd inner loop and checks the condition if true then it displays the charter”*”.

4) The second outer for loop will display this pattern.

Output:

2. Using While Loop

1) while loop is entry checking loop,i.e it checks the code at while, if it is true it executes the code.

2) The 1st outer while loop executes the code until the condition i<=n false. if the condition true it checks the 1st inner while loop if condition true then it displays space, otherwise, goes to the next inner loop and display charter “*”.

3) 2nd outer while loop executes the code until the condition I>0.

Output:

 

3. Diamond Star Pattern Program – Using Do While Loop

  1. The do-while loop is exit checking loop,i.e it executes the code once then checks the condition.

2) 1st outer do-while loop executes the statements until the condition ++i<=n is false,the first inner loop will display space until the condition ++j<=n-i+1 is false, 2nd inner loop will display “*” until the condition++j<=i*2-1 is false. The 1st outer do-while loop will display half of the diamond pattern.

3) The 2nd outer do-while loop will execute the statements until the condition –i>0 if false. The 1st inner loop will display space until the condition ++j<=n-i+1 is false,The 2nd inner loop will display charter “*” until the condition ++j<=i*2-1 is false.The 2nd outer do-while loop will display the remaining half of the pattern of the diamond.

Output:
That’s it.

You can learn Star Pattern Programs in Java Here.

Other Programs In Java:

x

Check Also

Reverse A Number In Java – 4 Simple Ways | Programs

Reverse A Number In Java – We have discussed the various methods to reverse a ...