Hollow Diamond Pattern in Java
In the given example, you will learn Java programming code to print Hollow Diamond Pattern. Such type of logical question is generally asked in an interview or in a competitive exam. This can be helpful to improve your logical programming skill and helpful in game development and other logical application development.
The following Java program prints Hollow Diamond Pattern made up of stars up to the specified n lines. In this Java program, we take the row number in input from the client. Here the row demonstrates a number of lines that will be printed in one triangle shape of the Hollow Diamond design. Suppose we got the input row value of 7, then the total numbers of lines in the Hollow diamond pattern will be 13.
Hollow Diamond Pattern Java Program using for loop
The given Java program uses nested for loops to return the hollow diamond pattern -
//Hollow diamond pattern program
import java.util.Scanner;
public class HollowDiamondPattern{
public static void main(String args[]){
int x,y;
Scanner scan=new Scanner(System.in);
System.out.print("Enter the number of rows : ");
int rows=scan.nextInt();
//print upper triangle
for(x=1; x<=rows; x++){
for(y=rows; y>x; y--){
System.out.print(" ");//print space
}
System.out.print("*"); //print star
for(y=1; y<(x-1)*2; y++){
System.out.print(" ");
}
if(x==1){
System.out.print("\n");//move to next line
}
else{
System.out.print("*\n");
}
}
//print lower triangle
for(x=rows-1; x>=1; x--){
for(y=rows; y>x; y--){
System.out.print(" ");
}
System.out.print("*");
for(y=1; y<(x-1)*2; y++){
System.out.print(" ");
}
if(x==1){
System.out.print("\n");
}
else{
System.out.print("*\n");
}
}
}
}
Output of the above code:
Enter the number of rows 7
*
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
*
Hollow Diamond Pattern Java Program using while loop
The given Java program uses nested while loop to return the hollow diamond pattern -
//Hollow diamond pattern program
import java.util.Scanner;
public class HollowDiamondPattern{
public static void main(String args[]){
int x,y;
Scanner scan=new Scanner(System.in);
System.out.println("Enter the number of rows");
int rows=scan.nextInt();
x=1;
//print upper triangle
while(x<=rows){
y=rows;
while( y>x){
System.out.print(" ");
y--;
}
System.out.print("*");
y=1;
while(y<(x-1)*2){
System.out.print(" ");
y++;
}
if(x==1){
System.out.print("\n");
}
else{
System.out.print("*\n");
}
x++;
}
//print lower triangle
x=rows-1;
while(x>=1){
y=rows;
while(y>x){
System.out.print(" ");//print space
y--;
}
System.out.print("*");//print star
y=1;
while(y<(x-1)*2){
System.out.print(" ");
y++;
}
if(x==1){
System.out.print("\n");//move to next line
}
else{
System.out.print("*\n");
}
x--;
}
}
}
Output of the above code:
Enter the number of rows
*
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
*
Related Articles
Sort array in ascending order JavaAutomorphic number in Java
Pascal triangle program in Java
Factorial using recursion in java
Java random number between 1 and 10
Palindrome program in Java
Floyd triangle in Java
Pyramid pattern programs in Java
Star pattern programs in Java
Number pattern programs in Java
Java program to find area of rectangle
Matrix multiplication in Java
Electricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java