Updated February 24, 2023
Definition of Hollow Diamond Pattern in Java
The hollow diamond pattern in java provides an illusion of a Diamond with a hollow interior. The pattern consists of a pyramid and a reverse pyramid and is symmetrical. Programmers widely write Java pattern programs to enhance their coding skills and ace interviews. This pattern is often asked in interviews to check the implementation and logical thinking of the programmer. In a hollow diamond pattern, the last and first rows will contain the start, and the rest row will contain two stars. Using a hollow diamond pattern, we can replace start (*) with any other symbol we want to print.
Introduction to Hollow Diamond Pattern in Java
The hollow java pattern in java is a program that generates the diamond shape, and that was made from (*) or any other characters. Using this pattern, the outer edges are filled with the characters, and the inner area is empty.
- This pattern consists of two halves; the first is the upper half composed of n rows that gradually increase in width, and the second is the lower half composed of n-1 rows that gradually decrease in width; both are symmetrical and identical. It can be customized with any size.
- Each diamond row is divided into two sections: the left section, which is made up of spaces that reduce in number from the first row to the center row and then increase once more to the last row, and the right section, which is formed of asterisks, increase in number from the first row to the center row and then decrease in number again until they reach the last row. The left and right parts are divided by a hollow area in the center, which is filled with spaces.
Key Takeaways
- The hollow diamond java pattern is generated by using conditions, loops, and variables. This program can output the pattern to the console or in the graphical user interface.
- We can generate a more complex diamond pattern by using nested loops. It’s a common programming exercise and is also used to develop programming skills.
Examples to Create Hollow Diamond Pattern in Java
We can create using multiple ways. The strategy to draw the diamond pattern in java is that we need to print the spaces before and after the star; this is used to increase the spacing between stars.
Algorithm:
- Start
- If the num is odd, then raise it.
- Then locate the mid = num/2
- After locating the mid, go 1 to the middle to print the design of the top half (say s)
- To print the upper left corner spaces, go from 1 to mid-s (sat t).
- Print the user-entered symbol if s == 1.
- At the time loop is complete, print the symbol and then traverse from 1 to 2*s-3 to print the gaps of a hollow diamond.
- Then navigate from 1 to mid-s and then print the spaces one more time for the box in the upper right corner.
- To print the lower half design, go from mid+1 to n-1.
- Then move the cursor from 1 to s-mid for the lower outermost rectangle.
- If (I == num – 1), then print the symbol.
- If not, then print the symbol and then travel from 1 to 2*s-3 to print the gaps of the hollow diamond, and then print the * after the loop is finished.
- For the lowest outside box, go from 1 to s-mid and print the spaces.
- Then, finish step 9 to close the loop.
- Stop
Example #1: Using For Loop
We use for loop to design a hollow diamond pattern; for loop is used to print the pattern with an outer loop that iterates through each row of diamonds, and an inner loop is used to print the spaces and symbols for every row. Below we demonstrate a hollow diamond pattern by using for loop to print the * symbol and spaces.
Code:
import java.util.Scanner;
public class HDP
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter num of rows: ");
int r = sc.nextInt();
System.out.print("Enter symbol: ");
char smb = sc.next().charAt(0);
for(int s = 1; s s; t--)
{
System.out.print(" ");
}
System.out.print(smb);
for(int t = 1; t = 1; s--)
{
for(int t = r; t > s; t--)
{
System.out.print(" ");
}
System.out.print(smb);
for(int t = 1; t <(s-1)*2; t++)
{
System.out.print(" ");
}
if(s==1)
{
System.out.print("\n");
}
else
{
System.out.print(smb+"\n");
}
}
}
}
Output:
Example #2: Using While Loop
While loop is also used to draw a hollow diamond pattern in java. We iterate while loop to print the symbol and make the pattern. We executed the codes below to show a hollow diamond pattern using a while loop. Here we printed the pattern by using the $ symbol.
Code:
import java.util.Scanner;
public class HDP
{
public static void main(String args[])
{
int s, t;
Scanner sc = new Scanner(System.in);
System.out.print ("Enter the num: ");
int r = sc.nextInt();
System.out.print ("Enter the symbol: ");
char smb = sc.next().charAt(0);
s = 1;
while(s <= r)
{
t = r;
while(t > s)
{
System.out.print(" ");
t--;
}
System.out.print(smb);
t = 1;
while(t<(s-1)*2)
{
System.out.print (" ");
t++;
}
if(s == 1)
{
System.out.print ("\n");
}
else
{
System.out.print (smb+"\n");
}
s++;
}
s = r-1;
while( s >= 1)
{
t = r;
while(t > s)
{
System.out.print(" ");
t--;
}
System.out.print(smb);
t = 1;
while (t<(s-1)*2)
{
System.out.print (" ");
t++;
}
if (s == 1)
{
System.out.print ("\n");
}
else
{
System.out.print (smb+"\n");
}
s--;
}
}
}
Output:
Example #3: Using Do-While Loop
Do-while loop is used to design a hollow diamond pattern in java. The below code uses the # symbol pattern for execution. The output of the do-while loop is the same as the while and for loops, only the difference in the condition of the inner loop.
Code:
import java.util.Scanner;
public class HDP
{
public static void main(String args[])
{
int s, t;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the num: ");
int r = sc.nextInt();
System.out.print("Enter the symbol: ");
char smb = sc.next().charAt(0);
s = 1;
do
{
t = r;
do
{
System.out.print(" ");
t--;
}
while(t >= s);
System.out.print(smb);
t = 1;
do
{
System.out.print(" ");
t++;
}
while(t<(s-1)*2);
if(s == 1)
{
System.out.print("\n");
}
else
{
System.out.print(smb+"\n");
}
s++;
}
while(s <= r);
s = r-1;
do
{
t = r;
do
{
System.out.print(" ");
t--;
}
while(t >= s);
System.out.print(smb);
t = 1;
do
{
System.out.print(" ");
t++;
}
while (t <(s-1)*2);
if(s == 1)
{
System.out.print("\n");
}
else
{
System.out.print(smb+"\n");
}
s--;
}
while(s >= 1);
}
}
Output:
Conclusion
The hollow diamond pattern in java is a common exercise that can help to develop skills in programs such as conditions, loops, and variables. We can make the pattern in any symbol or can print the required characters in the desired positions, where we can fill the outer edges. The pattern can be modified by changing the number of rows, the diamond’s size, and the characters that were used to create it. We can generate a more complex pattern using loops.
FAQs
Q1. How can we generate a hollow diamond pattern in java?
Answer: We can generate by using conditions, nested loops, and variables. When creating the pattern, we can define any symbol of character to the pattern.
Q2. How can we customize the size of the hollow diamond pattern in java?
Answer: We can customize the size of the hollow diamond pattern in java by taking the user input and using the same to determine columns and rows in the diamond pattern.
Q3. What are the benefits of generating a hollow diamond pattern in java?
Answer: Generating hollow diamond patterns in java helps us to develop problem-solving skills and understand the fundamentals of programming, such as conditions, loops, and practical exercises.
Recommended Article
We hope this EDUCBA information on “Hollow Diamond Pattern in Java” was beneficial to you. You can view EDUCBA’s recommended articles for more information.