Write a Java Program to print the diamond pattern of stars, numbers, and alphabets using a for loop, while loop, do while loop, and functions with an example. The Diamond pattern is a combination of a pyramid and an inverted pyramid. So, you need two sets of nested loops, one for the pyramid and the other for the inverted one, to create the complete diamond.
Java Program to Print Diamond Star Pattern
The below Program accepts the user-given rows and prints a diamond star pattern using the nested for loop. As you can see in the code, we divided the code to print the normal triangle and inverted pyramid.
package ShapePrograms; import java.util.Scanner; public class DiamondPattern1 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); int i, j; System.out.print("Enter Diamond Pattern Rows = "); int rows = sc.nextInt(); System.out.println("Printing Diamond Star Pattern"); for (i = 1 ; i <= rows; i++ ) { for (j = 1 ; j <= rows - i; j++ ) { System.out.print(" "); } for (j = 1 ; j <= i * 2 - 1; j++ ) { System.out.print("*"); } System.out.println(); } for (i = rows - 1 ; i > 0; i-- ) { for (j = 1 ; j <= rows - i; j++ ) { System.out.print(" "); } for (j = 1 ; j <= i * 2 - 1; j++ ) { System.out.print("*"); } System.out.println(); } } }
In this Diamond Pattern of Stars program, we replaced the for loop with a while loop to perform the iterations of rows and columns.
package ShapePrograms; import java.util.Scanner; public class DiamondPattern3 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); int i, j, k; System.out.print("Enter Rows = "); int rows = sc.nextInt(); i = 1 ; while(i <= rows ) { j = 1 ; while( j <= rows - i ) { System.out.print(" "); j++; } k = 1 ; while ( k <= i * 2 - 1 ) { System.out.print("*"); k++; } System.out.println(); i++; } i = rows - 1 ; while( i > 0 ) { j = 1 ; while ( j <= rows - i ) { System.out.print(" "); j++; } k = 1 ; while( k <= i * 2 - 1 ) { System.out.print("*"); k++; } System.out.println(); i--; } } }
Enter Rows = 12
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
*********************
***********************
*********************
*******************
*****************
***************
*************
***********
*********
*******
*****
***
*
This example program uses the do while loop to iterate the rows and columns and prints the Diamond Star Pattern.
package ShapePrograms; import java.util.Scanner; public class DiamondPattern4 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); int i, j, k; System.out.print("Enter Diamond Pattern Rows = "); int rows = sc.nextInt(); i = 1 ; do { j = 1 ; do { System.out.print(" "); } while( j++ <= rows - i ) ; k = 1 ; do { System.out.print("*"); } while ( ++k <= i * 2 - 1 ) ; System.out.println(); } while(++i <= rows ) ; i = rows - 1 ; do { j = 1 ; do { System.out.print(" "); } while (j++ <= rows - i ) ; k = 1 ; do { System.out.print("*"); } while( ++k <= i * 2 - 1 ) ; System.out.println(); } while( --i > 0 ) ; } }
Enter Diamond Pattern Rows = 15
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
*********************
***********************
*************************
***************************
*****************************
***************************
*************************
***********************
*********************
*******************
*****************
***************
*************
***********
*********
*******
*****
***
*
In this example, the diamondPattern function accepts the rows and the character (or symbol) and prints the diamond pattern of a given symbol.
import java.util.Scanner; public class DiamondPattern2 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); System.out.print("Enter Diamond Pattern Rows = "); int rows = sc.nextInt(); System.out.print("Enter Character for Diamond Pattern = "); char ch = sc.next().charAt(0); diamondPattern(rows, ch); } public static void diamondPattern(int rows, char ch) { int i, j, k; int x = rows - 1; int y = 1; for (i = 1 ; i <= rows; i++ ) { for (j = 1 ; j <= x; j++ ) { System.out.print(" "); } for (k = 1 ; k <= y; k++ ) { System.out.print(ch); } if(x > i) { x = x - 1; y = y + 2; } if(x < i) { x = x + 1; y = y - 2; } System.out.println(); } } }
Enter Diamond Pattern Rows = 10
Enter Character for Diamond Pattern = #
#
###
#####
#######
#########
#########
#######
#####
###
#
Hollow Diamond Star Pattern
For more information on hollow example >> Click Here.
import java.util.Scanner; public class Example { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); int i; System.out.print("Enter rows = "); int rw = sc.nextInt(); for (i = 1; i <= rw; i++) { printItems(rw, i); } for (i = rw - 1; i > 0; i--) { printItems(rw, i); } } public static void printItems(int rw, int i) { for (int j = 1; j <= rw - i; j++) { System.out.print(" "); } for (int k = 1; k <= i * 2 - 1; k++) { if (k == 1 || k == i * 2 - 1) { System.out.print("*"); } else { System.out.print(" "); } } System.out.println(); } }
Output
Enter rows = 12
*
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
*
Half Diamond Star Pattern
For more information on half example >> Click Here.
import java.util.Scanner; public class Example { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); int i; System.out.print("Enter rows = "); int rw = sc.nextInt(); for (i = 1; i <= rw; i++) { printItems(rw, i); } for (i = rw - 1; i > 0; i--) { printItems(rw, i); } } public static void printItems(int rw, int i) { for (int j = 1; j <= i; j++) { System.out.print("*"); } System.out.println(); } }
Output
Enter rows = 14
*
**
***
****
*****
******
*******
********
*********
**********
***********
************
*************
**************
*************
************
***********
**********
*********
********
*******
******
*****
****
***
**
*
Mirrored Half Diamond Star Pattern
For more information on mirrored half >> Click Here.
import java.util.Scanner; public class Example { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); int i; System.out.print("Enter rows = "); int rw = sc.nextInt(); for (i = 1; i <= rw; i++) { printItems(rw, i); } for (i = rw - 1; i > 0; i--) { printItems(rw, i); } } public static void printItems(int rw, int i) { for (int j = 1; j <= rw - i; j++) { System.out.print(" "); } for (int k = 1; k <= i; k++) { System.out.print("*"); } System.out.println(); } }
Output
Enter rows = 9
*
**
***
****
*****
******
*******
********
*********
********
*******
******
*****
****
***
**
*
Hollow Diamond Pattern inside a Square
For more information on the Hollow Diamond Pattern inside a Square >> Click Here.
import java.util.Scanner; public class Example { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); int i, j, k; System.out.print("Enter Rows = "); int rw = sc.nextInt(); for (i = 1; i <= rw; i++) { for (j = i; j <= rw; j++) { System.out.print("*"); } for (j = 1; j <= 2 * i - 2; j++) { System.out.print(" "); } for (k = i; k <= rw; k++) { System.out.print("*"); } System.out.println(); } for (i = 1; i <= rw; i++) { for (j = 1; j <= i; j++) { System.out.print("*"); } for (k = 2 * i - 2; k < 2 * rw - 2; k++) { System.out.print(" "); } for (k = 1; k <= i; k++) { System.out.print("*"); } System.out.println(); } } }
Output
Enter Rows = 10
********************
********* *********
******** ********
******* *******
****** ******
***** *****
**** ****
*** ***
** **
* *
* *
** **
*** ***
**** ****
***** *****
****** ******
******* *******
******** ********
********* *********
********************
Java Program to Print Diamond Pattern of Numbers
All the above programs print various types of Dimond patterns filled with stars, however, this section shows how to fill them with numbers. For more information >> Click Here.
import java.util.Scanner; public class Example { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); int i; System.out.print("Enter rows = "); int rw = sc.nextInt(); for (i = 1; i <= rw; i++) { printItems(rw, i); } for (i = rw - 1; i > 0; i--) { printItems(rw, i); } } public static void printItems(int rw, int i) { for (int j = 1; j <= rw - i; j++) { System.out.print(" "); } for (int k = 1; k <= i * 2 - 1; k++) { System.out.print(k); } System.out.println(); } }
Output
Enter rows = 5
1
123
12345
1234567
123456789
1234567
12345
123
1
Java Program to Print Diamond Pattern of Alphabets
This example starts with the alphabet A and prints the diamond pattern pattern with. the remaing alphabets. For more information >> Click Here.
import java.util.Scanner; public class Example { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); int i; System.out.print("Enter Rows = "); int rw = sc.nextInt(); for (i = 1; i <= rw; i++) { printItems(rw, i); } for (i = rw - 1; i > 0; i--) { printItems(rw, i); } } public static void printItems(int rw, int i) { int alphabet = 64; for (int j = 1; j <= rw - i; j++) { System.out.print(" "); } for (int k = 1; k <= i * 2 - 1; k++) { System.out.print((char) (alphabet + k)); } System.out.println(); } }
Output
Enter Rows = 15
A
ABC
ABCDE
ABCDEFG
ABCDEFGHI
ABCDEFGHIJK
ABCDEFGHIJKLM
ABCDEFGHIJKLMNO
ABCDEFGHIJKLMNOPQ
ABCDEFGHIJKLMNOPQRS
ABCDEFGHIJKLMNOPQRSTU
ABCDEFGHIJKLMNOPQRSTUVW
ABCDEFGHIJKLMNOPQRSTUVWXY
ABCDEFGHIJKLMNOPQRSTUVWXYZ[
ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]
ABCDEFGHIJKLMNOPQRSTUVWXYZ[
ABCDEFGHIJKLMNOPQRSTUVWXY
ABCDEFGHIJKLMNOPQRSTUVW
ABCDEFGHIJKLMNOPQRSTU
ABCDEFGHIJKLMNOPQRS
ABCDEFGHIJKLMNOPQ
ABCDEFGHIJKLMNO
ABCDEFGHIJKLM
ABCDEFGHIJK
ABCDEFGHI
ABCDEFG
ABCDE
ABC
A