In this “Hollow Diamond Pattern” – We have written Java programs to print/draw Star (X) pattern by using different types of Java loops and you can execute and try these Java program through your IDE. This is most [100+] Frequently Asked Java Program on beginner level to check you Java Programming skills and Concept.
Logic
In the Hollow Diamond Pattern following this logic,
- Hollow Diamond will draw in two phases upper part and lower part (just opposite logic)
- First and last row will have only one symbol.
- Consider spaces from left before print symbol and spaces before second symbol
- Once last symbol print jump cursor to next row (\n)

This post covers following ways to print Hollow Diamond Pattern:
- Hollow Diamond Pattern Java Program: Using for Loop
- Hollow Diamond Pattern Java Program: Using While Loop
- Hollow Diamond Pattern Java Program : Using Do-While Loop
Hollow Diamond Pattern Java Program: For Loop
This Hollow Diamond Java Program is following the below for loop logic:
package _1_patterns.hollow_diamond;
import java.util.Scanner;
public class HollowDiamondPatterForLoop {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter Pattern Size : ");
int size = sc.nextInt();
System.out.print("Enter Symbol : ");
char symbol = sc.next().charAt(0);
drawHollowDiamondPattern(size, symbol);
// Close Scanner
sc.close();
}
private static void drawHollowDiamondPattern(int n, char c) {
// prints upper section of the pattern
for (int i = 1; i <= n; i++) {
// print spaces from left
for (int j = n; j > i; j--) {
System.out.print(" ");
}
// prints symbol
System.out.print(c);
// print spaces in diamond
for (int j = 1; j < (i - 1) * 2; j++) {
System.out.print(" ");
}
if (i == 1) {
// throws cursor to the next line as first row will have one symbol
System.out.print("\n");
} else {
// prints symbol and throws cursor to the next line
System.out.print(c + "\n");
}
}
// prints lower section of the pattern
for (int i = n - 1; i >= 1; i--) {
// print spaces from left
for (int j = n; j > i; j--) {
System.out.print(" ");
}
// prints symbol
System.out.print(c);
// print spaces of hollow diamond
for (int j = 1; j < (i - 1) * 2; j++) {
System.out.print(" ");
}
if (i == 1) {
// throws cursor to the next line because last row will have one symbol
System.out.print("\n");
} else {
// prints symbol and throws cursor to the next line
System.out.print(c + "\n");
}
}
}
}
Output
Enter Pattern Size : 8 Enter Symbol : X X X X X X X X X X X X X X X X X X X X X X X X X X X X X
Hollow Diamond Pattern Java Program: While Loop
This Hollow Diamond Java Program is following the below while loop logic:
package _1_patterns.hollow_diamond;
import java.util.Scanner;
public class HollowDiamondPatterWhileLoop {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter Pattern Size : ");
int size = sc.nextInt();
System.out.print("Enter Symbol : ");
char symbol = sc.next().charAt(0);
drawHollowDiamondPattern(size, symbol);
// Close Scanner
sc.close();
}
private static void drawHollowDiamondPattern(int n, char c) {
// prints upper section of the pattern
int i = 1;
while (i <= n) {
// print spaces from left
int j = n;
while (j > i) {
System.out.print(" ");
j--;
}
// prints symbol
System.out.print(c);
// print spaces in diamond
j = 1;
while (j < (i - 1) * 2) {
System.out.print(" ");
j++;
}
if (i == 1) {
// throws cursor to the next line as first row will have one symbol
System.out.print("\n");
} else {
// prints symbol and throws cursor to the next line
System.out.print(c + "\n");
}
i++;
}
// prints lower section of the pattern
i = n - 1;
while (i >= 1) {
// print spaces from left
int j = n;
while (j > i) {
System.out.print(" ");
j--;
}
// prints symbol
System.out.print(c);
// print spaces of hollow diamond
j = 1;
while (j < (i - 1) * 2) {
System.out.print(" ");
j++;
}
if (i == 1) {
// throws cursor to the next line because last row will have one symbol
System.out.print("\n");
} else {
// prints symbol and throws cursor to the next line
System.out.print(c + "\n");
}
i--;
}
}
}
Output
Enter Pattern Size : 8 Enter Symbol : & & & & & & & & & & & & & & & & & & & & & & & & & & & & &
Hollow Diamond Pattern Java Program: Do-While Loop
This Hollow Diamond Java Program is following the below do-while loop logic:
package _1_patterns.hollow_diamond;
import java.util.Scanner;
public class HollowDiamondPatterDoWhileLoop {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter Pattern Size : ");
int size = sc.nextInt();
System.out.print("Enter Symbol : ");
char symbol = sc.next().charAt(0);
drawHollowDiamondPattern(size, symbol);
// Close Scanner
sc.close();
}
private static void drawHollowDiamondPattern(int n, char c) {
// prints upper section of the pattern
int i = 1;
do {
// print spaces from left
int j = n;
do {
System.out.print(" ");
j--;
} while (j > i);
// prints symbol
System.out.print(c);
// print spaces in diamond
j = 1;
do {
System.out.print(" ");
j++;
} while (j < (i - 1) * 2);
if (i == 1) {
// throws cursor to the next line as first row will have one symbol
System.out.print("\n");
} else {
// prints symbol and throws cursor to the next line
System.out.print(c + "\n");
}
i++;
} while (i <= n);
// prints lower section of the pattern
i = n - 1;
do {
// print spaces from left
int j = n;
do {
System.out.print(" ");
j--;
} while (j > i);
// prints symbol
System.out.print(c);
// print spaces of hollow diamond
j = 1;
do {
System.out.print(" ");
j++;
} while (j < (i - 1) * 2);
if (i == 1) {
// throws cursor to the next line because last row will have one symbol
System.out.print("\n");
} else {
// prints symbol and throws cursor to the next line
System.out.print(c + "\n");
}
i--;
} while (i >= 1);
}
}
Output
Enter Pattern Size : 8 Enter Symbol : % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
Related Posts
Your Feedback Motivate Us
If our FacingIssuesOnIT Experts solutions guide you to resolve your issues and improve your knowledge. Please share your comments, like and subscribe to get notifications for our posts.
Happy Learning !!!
You must log in to post a comment.