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.
1 2 3 4 5 |
********** **** **** *** *** ** ** * * |
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.
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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
import java.util.Scanner; public class HDiamond { 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=0;i<n;i++) { for(int j=0;j<n;j++) { if(j<n-i) System.out.print(c); else System.out.print(" "); } for(int j=0;j<n;j++) { if(j<i) System.out.print(" "); else System.out.print(c); } System.out.println(); } // for(int i=1;i<=n;i++) { for(int j=0;j<n;j++) { if(j<i) System.out.print(c); else System.out.print(" "); } for(int j=0;j<n;j++) { if(j<n-i) System.out.print(" "); else System.out.print(c); } System.out.println(); } } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Enter N : 5 Enter Symbol : * ********** **** **** *** *** ** ** * * * * ** ** *** *** **** **** ********** |
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.
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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
import java.util.Scanner; public class HDiamond { 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=0; int j; while(i<n) { j=0; while(j<n) { if(j<n-i) System.out.print(c); else System.out.print(" "); j++; } j=0; while(j<n) { if(j<i) System.out.print(" "); else System.out.print(c); j++; } System.out.println(); i++; } i=1; while(i<=n) { j=0; while(j<n) { if(j<i) System.out.print(c); else System.out.print(" "); j++; } j=0; while(j<n) { if(j<n-i) System.out.print(" "); else System.out.print(c); j++; } System.out.println(); i++; } } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Enter N : 5 Enter Symbol : . .......... .... .... ... ... .. .. . . . . .. .. ... ... .... .... .......... |
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.
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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
import java.util.Scanner; public class HDiamond { 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=0; int j; //while(i<n) do { j=0; do { if(j<n-i) System.out.print(c); else System.out.print(" "); j++; }while(j<n); j=0; do { if(j<i) System.out.print(" "); else System.out.print(c); j++; }while(j<n); System.out.println(); i++; } while(i<n); i=1; do { j=0; do { if(j<i) System.out.print(c); else System.out.print(" "); j++; } while(j<n); j=0; do { if(j<n-i) System.out.print(" "); else System.out.print(c); j++; }while(j<n); System.out.println(); i++; }while(i<=n); } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Enter N : 5 Enter Symbol : + ++++++++++ ++++ ++++ +++ +++ ++ ++ + + + + ++ ++ +++ +++ ++++ ++++ ++++++++++ |
For More: