PUBLISHED ON: APRIL 16, 2021
Java Program to Print the Hollow Diamond Pattern
In this tutorial, we will see how to print the hollow diamond pattern in Java. First, we will ask the user to initialize the number of rows. Then, we will use loops to print two hollow equilateral triangles facing away from each other but with the same base. But before moving further, if you are not familiar with the concept of the loops in java, then do check the article on Loops in Java.
Input: Enter the number of rows: 4
Output: The pattern is:
*
* *
* *
* *
*
This can be done by using the following methods:
Approach 1: Using For Loop
Approach 2: Using While loop
Let us look at each of these approaches for a better understanding.
Program 1: Print the Hollow Diamond Pattern
In this program, we will see how to print the hollow diamond pattern in Java using a for loop.
Algorithm:
- Start
- Create an instance of the Scanner class.
- Declare a variable to store the number of rows.
- Ask the user to initialize the variable.
- Use two for loops to print the pattern.
- Inside each for loop use two more for loops and an if statement to print the spaces and pattern.
- Use the first for loop to print the upper hollow diamond pattern.
- Use the second for loop to print the lower hollow diamond pattern.
- Display the result.
- Stop
The below example illustrates the implementation of the above algorithm.
//Java Program to print the hollow diamond pattern
import java.util.Scanner;
public class Main
{
public static void main(String args[])
{
int i,j;
Scanner scan=new Scanner(System.in);
System.out.println("Enter the number of rows");
int rows=scan.nextInt();//get input from user
//print upper triangle
for(i=1; i<=rows; i++)
{
for(j=rows; j>i; j--)
{
System.out.print(" ");//print space
}
System.out.print("*"); //print star
for(j=1; j<(i-1)*2; j++)
{
System.out.print(" ");
}
if(i==1)
{
System.out.print("\n");//move to next line
}
else
{
System.out.print("*\n");
}
}
//print lower triangle
for(i=rows-1; i>=1; i--)
{
for(j=rows; j>i; j--)
{
System.out.print(" ");
}
System.out.print("*");
for(j=1; j<(i-1)*2; j++)
{
System.out.print(" ");
}
if(i==1)
{
System.out.print("\n");
}
else
{
System.out.print("*\n");
}
}
}
}
Enter the number of rows: 7
*
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
*
Program 2: Print the Hollow Diamond Pattern
In this program, we will see how to print the hollow diamond pattern in Java using a while loop.
Algorithm:
- Start
- Create an instance of the Scanner class.
- Declare variables to store the number of rows and the pattern symbol.
- Ask the user to initialize these variables.
- Use two while loops to print the pattern.
- Inside each while loop use two more while and an if statement to print the spaces and pattern.
- Use the first while loop to print the upper hollow diamond pattern.
- Use the second while loop to print the lower hollow diamond pattern.
- Display the result.
- Stop
The below example illustrates the implementation of the above algorithm.
//Java Program to print the hollow diamond pattern
import java.util.Scanner;
public class Main
{
public static void main(String args[])
{
int i,j;
Scanner scan=new Scanner(System.in);
System.out.println("Enter the number of rows");
int rows=scan.nextInt(); //get input from user
i=1;
//print upper triangle
while(i<=rows)
{
j=rows;
while(j>i)
{
System.out.print(" ");
j--;
}
System.out.print("*");
j=1;
while(j<(i-1)*2)
{
System.out.print(" ");
j++;
}
if(i==1)
{
System.out.print("\n");
}
else
{
System.out.print("*\n");
}
i++;
}
//print lower triangle
i=rows-1;
while(i>=1)
{
j=rows;
while(j>i)
{
System.out.print(" "); //print space
j--;
}
System.out.print("*"); //print star
j=1;
while(j<(i-1)*2)
{
System.out.print(" ");
j++;
}
if(i==1)
{
System.out.print("\n"); //move to next line
}
else
{
System.out.print("*\n");
}
i--;
}
}
}
Enter the number of rows: 7
*
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
*