➤ Check Even Number
➤ Check Odd Number
➤ Java Even-Odd
➤ Greatest of 3 numbers
➤ Exponents in Java
➤ Java Leap Year Program
➤ Display Multiplication Table
➤ Reverse of a number
➤ Factors of a Number
➤ Java LCM of 2 Numbers
➤ Java HCF of 2 Numbers
➤ Quadratic Equation Program
➤ Square Root of Number
➤ Perfect Square Program
➤ Simple Calculator Program
➤ BMI Calculator Java
➤ Factorial of a Number
➤ Factorial Using Recursion
# Java Programs to Find Sum
# Java Conversion Programs
# Java Program on Series
# Java Pattern Programs
# Java Number Programs
Java Array Programs
Java String Programs
Diamond Pattern Program in Java | There are different diamond pattern programs in Java, Here we will write Half diamond pattern, Full diamond pattern, Hollow diamond pattern program, Different diamond pattern programs with numbers. Now, let us start with a Full diamond pattern program with stars.
1. Display the given below full diamond pattern of stars using Java.
*
***
*****
*******
*********
*******
*****
***
*
The program for the above full diamond pattern of stars can be written as,
import java.util.Scanner;
public class DiamondPattern {
public static void main(String[] args) {
// take input
Scanner scan = new Scanner(System.in);
int n = 0;
System.out.print("Enter N value:: ");
n = scan.nextInt();
printStartDiamond(n);
}
private static void printStartDiamond(int n) {
if(n <= 0)
System.out.println("Enter Positive Number");
// for increasing portion
for(int i=1; i<=n; i++) {
// print space
for(int j = i; j<=n; j++) {
System.out.print(" ");
}
// print star
for(int k = 1; k <= 2*i-1; k++) {
System.out.print("*");
}
// new line
System.out.println();
}
// for decrement portion
for(int i=n-1; i >=1; i--) {
// print space
for(int j=n; j >= i; j--) {
System.out.print(" ");
}
// print star
for(int k=1; k <= 2*i-1; k++) {
System.out.print("*");
}
// new line
System.out.println();
}
}
}
2. This pattern is just the opposite of the previous pattern. Write a Java program to display the below pattern of space and stars on the console.
**********
**** ****
*** ***
** **
* *
** **
*** ***
**** ****
**********
The Java code for the above pattern can be written as,
import java.util.Scanner;
public class DiamondPattern {
public static void main(String[] args) {
// take input
Scanner scan = new Scanner(System.in);
int n = 0;
System.out.print("Enter N value:: ");
n = scan.nextInt();
printSpaceDiamond(n);
}
private static void printSpaceDiamond(int n) {
if(n <= 0)
System.out.println("Enter Positive Number");
// for first half portion (top to bottom)
for(int i=1; i <= n; i++) {
// print star
for(int j = i; j <= n; j++) {
System.out.print("*");
}
// print space
for(int k = 1; k < 2*i-1; k++) {
System.out.print(" ");
}
// print star
for(int l=i; l <= n; l++) {
System.out.print("*");
}
// new line
System.out.println();
}
// for second half portion
for(int i=n-1; i >= 1; i--) {
// print star
for(int j=n; j >= i; j--) {
System.out.print("*");
}
// print space
for(int k=1; k < 2*i-1; k++) {
System.out.print(" ");
}
// print star
for(int l=n; l >= i; l--) {
System.out.print("*");
}
// new line
System.out.println();
}
}
}
3. Write a Java program to display the given below full diamond of Numbers.
1
123
12345
1234567
123456789
1234567
12345
123
1
The Java program for the above pattern is,
import java.util.Scanner;
public class DiamondPattern {
public static void main(String[] args) {
// take input
Scanner scan = new Scanner(System.in);
int n = 0;
System.out.print("Enter N value:: ");
n = scan.nextInt();
printNumberDiamond(n);
}
private static void printNumberDiamond(int n) {
if(n <= 0)
System.out.println("Enter Positive Number");
// declare temp variable
int a = 0;
// for first half portion (top to bottom)
for(int i=1; i <= n; i++) {
// in each iteration a will start from 1
a = 1;
// print space
for(int j = i; j <= n; j++) {
System.out.print(" ");
}
// print digit
for(int k = 1; k <= 2*i-1; k++) {
System.out.print(a++);
}
// new line
System.out.println();
}
// for second half portion
for(int i=n-1; i >= 1; i--) {
// in each iteration a will start from 1
a = 1;
// print space
for(int j=n; j >= i; j--) {
System.out.print(" ");
}
// print digit
for(int k=1; k <= 2*i-1; k++) {
System.out.print(a++);
}
// new line
System.out.println();
}
}
}
4. Write a Java program to display the given below full diamond of Numbers start with 0 and ends with 0.
0
010
01210
0123210
012343210
01234543210
012343210
0123210
01210
010
0
The Java program for the above pattern is,
import java.util.Scanner;
public class DiamondPattern {
public static void main(String[] args) {
// take input
Scanner scan = new Scanner(System.in);
int n = 0;
System.out.print("Enter N value:: ");
n = scan.nextInt();
printNumberDiamond(n);
}
private static void printNumberDiamond(int n) {
if(n <= 0)
System.out.println("Enter Positive Number");
// declare temp variable
int a = 0;
// for first half portion (top to bottom)
for(int i=1; i <= n+1; i++) {
// print space
for(int j = i; j <= n; j++) {
System.out.print(" ");
}
// print digit
for(int k = 1; k <= 2*i-1; k++) {
if(k < i)
System.out.print(a++);
else if(k == i)
System.out.print(a);
else
System.out.print(--a);
}
// new line
System.out.println();
}
// for second half portion
for(int i=n; i >= 1; i--) {
// print space
for(int j=n; j >= i; j--) {
System.out.print(" ");
}
// print digit
for(int k=1; k <= 2*i-1; k++) {
if(k < i)
System.out.print(a++);
else if(k == i)
System.out.print(a);
else
System.out.print(--a);
}
// new line
System.out.println();
}
}
}
Hollow Diamond Pattern Program in Java
5. Write a Java program to display the given below hollow diamond of stars.
*
* *
* *
* *
* *
* *
* *
* *
* *
* *
*
The code for the above pattern is,
import java.util.Scanner;
public class HollowDiamond {
public static void main(String[] args) {
// take input
Scanner scan = new Scanner(System.in);
int n = 0;
System.out.print("Enter N value:: ");
n = scan.nextInt();
printStarHollowDiamond(n);
}
private static void printStarHollowDiamond(int n) {
if(n <= 0)
System.out.println("Enter Positive Number");
// for first half portion (top to bottom)
for(int i=1; i <= n+1; i++) {
// print space
for(int j = i; j <= n; j++) {
System.out.print(" ");
}
// print digit or space
for(int k = 1; k <= 2*i-1; k++) {
if(k==1 || k==(2*i-1) )
System.out.print("*");
else
System.out.print(" ");
}
// new line
System.out.println();
}
// for second half portion
for(int i=n; i >= 1; i--) {
// print space
for(int j=n; j >= i; j--) {
System.out.print(" ");
}
// print digit or space
for(int k=1; k <= 2*i-1; k++) {
if(k==1 || k==(2*i-1) )
System.out.print("*");
else
System.out.print(" ");
}
// new line
System.out.println();
}
}
}
6. Write a Java program to display the given below hollow diamond of numbers using Java.
1
2 2
3 3
4 4
5 5
4 4
3 3
2 2
1
The code for the above pattern is,
import java.util.Scanner;
public class HollowDiamond {
public static void main(String[] args) {
// take input
Scanner scan = new Scanner(System.in);
int n = 0;
System.out.print("Enter N value:: ");
n = scan.nextInt();
printNumberHollowDiamond(n);
}
private static void printNumberHollowDiamond(int n) {
if(n <= 0)
System.out.println("Enter Positive Number");
// declare temp variable
int a = 1;
// for first half portion (top to bottom)
for(int i=1; i <= n; i++) {
// print space
for(int j = i; j <= n; j++) {
System.out.print(" ");
}
// print digit and space
for(int k = 1; k <= 2*i-1; k++) {
if(k==1 || k==(2*i-1) )
System.out.print(a);
else
System.out.print(" ");
}
// increase temp variable
a++;
// new line
System.out.println();
}
// update temp variable
a = n-1;
// for second half portion
for(int i=n-1; i >= 1; i--) {
// print space
for(int j=n; j >= i; j--) {
System.out.print(" ");
}
// print digit and space
for(int k=1; k <= 2*i-1; k++) {
if(k==1 || k==(2*i-1) )
System.out.print(a);
else
System.out.print(" ");
}
// decrease temp variable
a--;
// new line
System.out.println();
}
}
}
Half Diamond Pattern
7. Write a Java program to display the given below half diamond pattern of stars.
*
**
***
****
*****
****
***
**
*
The star variable holds the number of stars in the nth row. In every row, the star variable is updated. When i>n then (2*n-i) will be negative so, the abs() function is used to find the absolute value of (2*n-i).
import java.util.Scanner;
public class HalfDiamond {
public static void main(String[] args) {
// take input
Scanner scan = new Scanner(System.in);
int n = 0;
System.out.print("Enter N value:: ");
n = scan.nextInt();
printStarHalfDiamond(n);
}
private static void printStarHalfDiamond(int n) {
if(n <= 0)
System.out.println("Enter Positive Number");
// declare temp variable
int star = 0;
for(int i=1; i < 2*n; i++) {
if(i < n) star = i;
else star = Math.abs(2*n - i);
// print star
for(int j = 1; j <= star; j++) {
System.out.print("*");
}
// new line
System.out.println();
}
}
}
8. Write a Java program to display the given below mirrored hollow diamond of stars. It is just the opposite of the previous one.
*
**
***
****
*****
****
***
**
*
The Java program for the above mirrored hollow diamond of stars,
import java.util.Scanner;
public class HalfDiamond {
public static void main(String[] args) {
// take input
Scanner scan = new Scanner(System.in);
int n = 0;
System.out.print("Enter N value:: ");
n = scan.nextInt();
printStarHalfDiamond(n);
}
private static void printStarHalfDiamond(int n) {
if(n <= 0)
System.out.println("Enter Positive Number");
// declare temp variable
int star = 0;
for(int i=1; i < 2*n; i++) {
// print star
for(int j = 1; j <= n; j++) {
if(j <= Math.abs(n-i))
System.out.print(" ");
else
System.out.print("*");
}
// new line
System.out.println();
}
}
}
9. Write a Java program to display the given below hollow diamond pattern of numbers and stars.
1
2*2
3*3*3
4*4*4*4
5*5*5*5*5
4*4*4*4
3*3*3
2*2
1
The Java code for the above pattern can be written as,
import java.util.Scanner;
public class HalfDiamond {
public static void main(String[] args) {
// take input
Scanner scan = new Scanner(System.in);
int n = 0;
System.out.print("Enter N value:: ");
n = scan.nextInt();
printStarNumberHalfDiamond(n);
}
private static void printStarNumberHalfDiamond(int n) {
if(n <= 0)
System.out.println("Enter Positive Number");
// declare temp variable
int place = 0;
for(int i=1; i < 2*n; i++) {
if(i < n) place = i;
else place = Math.abs(2*n-i);
// print star or number
for(int j = 1; j < 2*place; j++) {
if(j % 2 == 0)
System.out.print("*"); // star
else
System.out.print(place); // number
}
// new line
System.out.println();
}
}
}
10. Write a Java program to display the given below hollow diamond pattern of numbers.
1
12
123
1234
12345
1234
123
12
1
The Java code for the above pattern can be written as,
import java.util.Scanner;
public class HalfDiamond {
public static void main(String[] args) {
// take input
Scanner scan = new Scanner(System.in);
int n = 0;
System.out.print("Enter N value:: ");
n = scan.nextInt();
printNumberHalfDiamond(n);
}
private static void printNumberHalfDiamond(int n) {
if(n <= 0)
System.out.println("Enter Positive Number");
// declare temp variable
int place = 0, a = 0;
for(int i=1; i < 2*n; i++) {
// after every iteration
// initialize a with 1
a = 1;
if(i < n) place = i;
else place = Math.abs(2*n-i);
// print star or number
for(int j = 1; j <= place; j++) {
System.out.print(a++); // number
}
// new line
System.out.println();
}
}
}
11. Write a program to display the given below hollow diamond pattern of numbers.
1
123
12345
1234567
123456789
1234567
12345
123
1
The Java code for the above pattern can be written as,
import java.util.Scanner;
public class HalfDiamond {
public static void main(String[] args) {
// take input
Scanner scan = new Scanner(System.in);
int n = 0;
System.out.print("Enter N value:: ");
n = scan.nextInt();
printNumberHalfDiamond(n);
}
private static void printNumberHalfDiamond(int n) {
if(n <= 0)
System.out.println("Enter Positive Number");
// declare temp variable
int place = 0, a = 0;
for(int i=1; i < 2*n; i++) {
// after every iteration
// initialize a with 1
a = 1;
if(i < n) place = i;
else place = Math.abs(2*n-i);
// print star or number
for(int j = 1; j <= 2*place-1; j++) {
System.out.print(a++); // number
}
// new line
System.out.println();
}
}
}
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!