Generate hollow diamond star pattern in Java
- Home
- Diamond pattern
- Generate hollow diamond star pattern in Java
- On
- By
- 0 Comment
- Categories: Diamond pattern, star pattern
Generate hollow diamond star pattern in Java
Generate hollow diamond star pattern in Java
In this tutorial, we will discuss Generate hollow diamond star pattern in Java
Hollow diamond star pattern using for loop
In this program, we are going to learn how to display hollow diamond star pattern using for loop in Java programming language
Here, we display a hollow diamond star pattern program with coding using nested for loop and also we get input from the user using Scanner class in Java language.
The user can provide numbers as they wish and get the hollow diamond star pattern according to their input.
Program 1
import java.util.Scanner; class HollowDiamondStarPattern{ public static void main(String args[]){ int i,j; Scanner scan=new Scanner(System.in); System.out.print("Enter the number of rows: "); int rows=scan.nextInt();//get input from user //loop for display upper half part of the pattern for(i=1; i<=rows; i++){ for(j=i; j<=rows; j++){ System.out.print("*");//print star } for(j=1; j<=(2*i-2); j++){ System.out.print(" ");//print space } for(j=i; j<=rows; j++){ System.out.print("*");//print star } System.out.print("\n"); } //loop for printing lower half part of the pattern for(i=1; i<=rows; i++){ for(j=1; j<=i; j++){ System.out.print("*");//print star } for(j=(2*i-2); j<(2*rows-2); j++){ System.out.print(" ");//print space } for(j=1; j<=i; j++){ System.out.print("*"); } System.out.print("\n");//move to next line } } }
When the above code executed, it produces the following results
Enter the number of rows: 6 ************ ***** ***** **** **** *** *** ** ** * * ** ** *** *** **** **** ***** ***** ************
Hollow diamond star pattern using while loop
In this program, we are going to learn how to display hollow diamond star pattern using while loop or in Java programming language
Here, we display a hollow diamond star pattern program with coding using nested while loop and also we get input from the user using Scanner class in Java language
The user can provide numbers as they wish and get the hollow diamond star pattern according to their input
Program 2
import java.util.Scanner; class HollowDiamondStarPattern1{ public static void main(String args[]){ int i,j; Scanner scan=new Scanner(System.in); System.out.print("Enter the number of rows: "); int rows=scan.nextInt();//get input from user //loop for display upper half part of the pattern i=1; while(i<=rows){ j=i; while(j<=rows){ System.out.print("*");//print star j++; } j=1; while(j<=(2*i-2)){ System.out.print(" ");//print space j++; } j=i; while(j<=rows){ System.out.print("*");//print star j++; } System.out.print("\n"); i++; } //loop for printing lower half part of the pattern i=1; while(i<=rows){ j=1; while(j<=i){ System.out.print("*");//print star j++; } j=(2*i-2); while(j<(2*rows-2)){ System.out.print(" ");//print space j++; } j=1; while(j<=i){ System.out.print("*"); j++; } System.out.print("\n");//move to next line i++; } } }
When the above code executed, it produces the following results
Enter the number of rows: 6 ************ ***** ***** **** **** *** *** ** ** * * ** ** *** *** **** **** ***** ***** ************
Suggested for you
Operator in Java language
For loop in Java language
While loop in Java language
Nested for loop in Java language
Nested while loop in Java language