diamond pattern in java is one of the interesting pattern program for beginner and intermediate learners of programming.
If you see diamond pattern you will see it is combination of two triangle pattern that is upper and lower triangle.
Here we will see different diamond pattern programs in java.
We will start with basic pattern in java
1. Triangle Pattern
Let first see to draw upper pattern.
- Get No of rows to print
- loop to number or rows using outer loop
for (int row = 1; row <= rows; row++)
- print odd numbers of * in each column for
(int col = 1; col <= 2 * row - 1; col++)
- Go to next Line and repeat step 1 to 3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | import java.util.Scanner; import java.util.Scanner; class TrianglePattern1 { public static void main(String[] args) { System.out.println("Enter Number of rows "); Scanner scanner = new Scanner(System.in); int rows = scanner.nextInt(); for (int row = 1; row <= rows; row++) { for (int col = 1; col <= 2 * row - 1; col++) { System.out.print("*"); } System.out.println(""); } } } |
Output
1 2 3 4 5 6 7 | Enter Number of rows 5 * *** ***** ******* ********* |
This will Print above pattern.
But we require pattern should be at center.
2. Triangle Pattern at center
Lets modify above program and add appropriate space so that we can print center triangle.
- Get No of rows to print
- loop to number or rows using outer loop
for (int row = 1; row <= rows; row++)
- Print number of spaces using this loop
for (int col = 1; col <= rows-row; col++)
- print odd numbers of * in each column for
(int col = 1; col <= 2 * row - 1; col++)
- Go to next line and repeat step 1-3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | import java.util.Scanner; class TrianglePattern1 { public static void main(String[] args) { System.out.println("Enter Number of rows "); Scanner scanner = new Scanner(System.in); int rows = scanner.nextInt(); for (int row = 1; row <= rows; row++) { for (int col = 1; col <= rows-row; col++) { System.out.print(" "); } for (int col = 1; col <= 2 * row - 1; col++) { System.out.print("*"); } System.out.println(""); } } } |
Output
1 2 3 4 5 6 7 | Enter Number of rows 5 * *** ***** ******* ********* |
This will print correct start pattern.
3. Lower Triangle Pattern
Lets see another program to print lower triangle program.
This program is similar to upper triangle only we have to reverse the outer loop
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | import java.util.Scanner; class LowerTrianglePattern1 { public static void main(String[] args) { System.out.println("Enter Number of rows "); Scanner scanner = new Scanner(System.in); int rows = scanner.nextInt(); for (int row = rows; row > 0; row--) { for (int col = 1; col <= rows - row; col++) { System.out.print(" "); } for (int col = 1; col <= 2 * row - 1; col++) { System.out.print("*"); } System.out.println(""); } } } |
Output
1 2 3 4 5 6 7 | Enter Number of rows 5 ********* ******* ***** *** * |
This is printing a good lower start triangle.
4. Diamond Pattern
Lets combine both upper and lower star triangle to make diamond.
Our AIM is to print diamond pattern of stars.
Lets see java program to print diamond pattern of stars .
Here Upper triangle is as it is only in lower triangle we started outer loop from one less position that is row=rows-1
for (int row = rows - 1; row > 0; row--)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | import java.util.Scanner; class DimondPattern1 { public static void main(String[] args) { System.out.println("Enter Number of rows "); Scanner scanner = new Scanner(System.in); int rows = scanner.nextInt(); for (int row = 1; row <= rows; row++) { for (int col = 1; col <= rows - row; col++) { System.out.print(" "); } for (int col = 1; col <= 2 * row - 1; col++) { System.out.print("*"); } System.out.println(""); } for (int row = rows - 1; row > 0; row--) { for (int col = 1; col <= rows - row; col++) { System.out.print(" "); } for (int col = 1; col <= 2 * row - 1; col++) { System.out.print("*"); } System.out.println(""); } } } |
Output
1 2 3 4 5 6 7 8 9 10 11 | Enter Number of rows 5 * *** ***** ******* ********* ******* ***** *** * |
After combining upper and lower triangle we got our diamond pattern.
5. Diamond Pattern with variation
As we know the concept of creating upper and lower triangle pattern.
we can replace * with any numbers les dive in with combining other symbols.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | import java.util.Scanner; class DimondPattern2 { public static void main(String[] args) { System.out.println("Enter Number of rows "); Scanner scanner = new Scanner(System.in); int rows = scanner.nextInt(); for (int row = 1; row <= rows; row++) { for (int col = 1; col <= rows - row; col++) { System.out.print(" "); } for (int col = 1; col <= 2 * row - 1; col++) { System.out.print("*"); } System.out.println(""); } for (int row = rows - 1; row > 0; row--) { for (int col = 1; col <= rows - row; col++) { System.out.print(" "); } for (int col = 1; col <= 2 * row - 1; col++) { System.out.print("#"); } System.out.println(""); } } } |
Output
1 2 3 4 5 6 7 8 9 10 11 | Enter Number of rows 5 * *** ***** ******* ********* ####### ##### ### # |
Let combine other symbols.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | import java.util.Scanner; class DimondPattern3 { public static void main(String[] args) { System.out.println("Enter Number of rows "); Scanner scanner = new Scanner(System.in); int rows = scanner.nextInt(); for (int row = 1; row <= rows-1; row++) { for (int col = 1; col <= rows - row; col++) { System.out.print(" "); } for (int col = 1; col <= 2 * row - 1; col++) { System.out.print("#"); } System.out.println(""); } for (int row = rows ; row > 0; row--) { for (int col = 1; col <= rows - row; col++) { System.out.print(" "); } for (int col = 1; col <= 2 * row - 1; col++) { System.out.print("$"); } System.out.println(""); } } } |
Output
1 2 3 4 5 6 7 8 9 10 11 | Enter Number of rows 5 # ### ##### ####### $$$$$$$$$ $$$$$$$ $$$$$ $$$ $ |
6. Diamond Pattern altering column
6.1 Altering Lower Triangle columns
Here upper triangle printing one time less then upper triangle.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | import java.util.Scanner; class DimondPattern3 { public static void main(String[] args) { System.out.println("Enter Number of rows "); Scanner scanner = new Scanner(System.in); int rows = scanner.nextInt(); for (int row = 1; row <= rows - 1; row++) { for (int col = 1; col <= rows - row; col++) { System.out.print(" "); } for (int col = 1; col <= 2 * row - 1; col++) { System.out.print("#"); } System.out.println(""); } for (int row = rows; row > 0; row--) { for (int col = 1; col <= rows - row; col++) { System.out.print(" "); } for (int col = 1; col <= 2 * row - 1; col++) { if (col % 2 == 0) { System.out.print("#"); } else { System.out.print("$"); } } System.out.println(""); } } } |
Output
1 2 3 4 5 6 7 8 9 10 11 | Enter Number of rows 5 # ### ##### ####### $#$#$#$#$ $#$#$#$ $#$#$ $#$ $ |
6.2 Altering Complete diamond pattern
Here two symbols are changed alternately in lower triangle.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | import java.util.Scanner; class DimondPattern3 { public static void main(String[] args) { System.out.println("Enter Number of rows "); Scanner scanner = new Scanner(System.in); int rows = scanner.nextInt(); for (int row = 1; row <= rows - 1; row++) { for (int col = 1; col <= rows - row; col++) { System.out.print(" "); } for (int col = 1; col <= 2 * row - 1; col++) { if (col % 2 == 0) { System.out.print("#"); } else { System.out.print("$"); } } System.out.println(""); } for (int row = rows; row > 0; row--) { for (int col = 1; col <= rows - row; col++) { System.out.print(" "); } for (int col = 1; col <= 2 * row - 1; col++) { if (col % 2 == 0) { System.out.print("#"); } else { System.out.print("$"); } } System.out.println(""); } } } |
Output
1 2 3 4 5 6 7 8 9 10 11 | Enter Number of rows 5 $ $#$ $#$#$ $#$#$#$ $#$#$#$#$ $#$#$#$ $#$#$ $#$ $ |
7. Diamond Pattern with row alteration
7.1 Altering lower triangle
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | import java.util.Scanner; class DimondPattern3 { public static void main(String[] args) { System.out.println("Enter Number of rows "); Scanner scanner = new Scanner(System.in); int rows = scanner.nextInt(); for (int row = 1; row <= rows - 1; row++) { for (int col = 1; col <= rows - row; col++) { System.out.print(" "); } for (int col = 1; col <= 2 * row - 1; col++) { System.out.print("#"); } System.out.println(""); } for (int row = rows; row > 0; row--) { for (int col = 1; col <= rows - row; col++) { System.out.print(" "); } for (int col = 1; col <= 2 * row - 1; col++) { if (row % 2 == 0) { System.out.print("#"); } else { System.out.print("$"); } } System.out.println(""); } } } |
Output
1 2 3 4 5 6 7 8 9 10 11 | Enter Number of rows 5 # ### ##### ####### $$$$$$$$$ ####### $$$$$ ### $ |
7.2 Altering all triangle row
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | import java.util.Scanner; class DimondPattern3 { public static void main(String[] args) { System.out.println("Enter Number of rows "); Scanner scanner = new Scanner(System.in); int rows = scanner.nextInt(); for (int row = 1; row <= rows - 1; row++) { for (int col = 1; col <= rows - row; col++) { System.out.print(" "); } for (int col = 1; col <= 2 * row - 1; col++) { if (row % 2 == 0) { System.out.print("#"); } else { System.out.print("$"); } } System.out.println(""); } for (int row = rows; row > 0; row--) { for (int col = 1; col <= rows - row; col++) { System.out.print(" "); } for (int col = 1; col <= 2 * row - 1; col++) { if (row % 2 == 0) { System.out.print("#"); } else { System.out.print("$"); } } System.out.println(""); } } } |
Output
1 2 3 4 5 6 7 8 9 10 11 | Enter Number of rows 5 $ ### $$$$$ ####### $$$$$$$$$ ####### $$$$$ ### $ |
8. Diamond Pattern with Numbers
8.1 Number column wise
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | import java.util.Scanner; class DimondPattern4 { public static void main(String[] args) { System.out.println("Enter Number of rows "); Scanner scanner = new Scanner(System.in); int rows = scanner.nextInt(); for (int row = 1; row <= rows; row++) { for (int col = 1; col <= rows - row; col++) { System.out.print(" "); } for (int col = 1; col <= 2 * row - 1; col++) { System.out.print(col); } System.out.println(""); } for (int row = rows - 1; row > 0; row--) { for (int col = 1; col <= rows - row; col++) { System.out.print(" "); } for (int col = 1; col <= 2 * row - 1; col++) { System.out.print(col); } System.out.println(""); } } } |
Output
1 2 3 4 5 6 7 8 9 10 11 | Enter Number of rows 5 1 123 12345 1234567 123456789 1234567 12345 123 1 |
8.2 Number row wise
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | import java.util.Scanner; class DimondPattern4 { public static void main(String[] args) { System.out.println("Enter Number of rows "); Scanner scanner = new Scanner(System.in); int rows = scanner.nextInt(); for (int row = 1; row <= rows; row++) { for (int col = 1; col <= rows - row; col++) { System.out.print(" "); } for (int col = 1; col <= 2 * row - 1; col++) { System.out.print(row); } System.out.println(""); } for (int row = rows - 1; row > 0; row--) { for (int col = 1; col <= rows - row; col++) { System.out.print(" "); } for (int col = 1; col <= 2 * row - 1; col++) { System.out.print(row); } System.out.println(""); } } } |
Output
1 2 3 4 5 6 7 8 9 10 11 | Enter Number of rows 5 1 222 33333 4444444 555555555 4444444 33333 222 1 |
9. Diamond Pattern for String
- Create a integer variable with value 64 ( we started loop with 1 so taken value 64)
- Inside loop print
System.out.print((char) (character + col));
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | import java.util.Scanner; class DimondPattern4 { public static void main(String[] args) { System.out.println("Enter Number of rows "); int character = 64; Scanner scanner = new Scanner(System.in); int rows = scanner.nextInt(); for (int row = 1; row <= rows; row++) { for (int col = 1; col <= rows - row; col++) { System.out.print(" "); } for (int col = 1; col <= 2 * row - 1; col++) { System.out.print((char) (character + col)); } System.out.println(""); } for (int row = rows - 1; row > 0; row--) { for (int col = 1; col <= rows - row; col++) { System.out.print(" "); } for (int col = 1; col <= 2 * row - 1; col++) { System.out.print((char) (character + col)); } System.out.println(""); } } } |
Output
1 2 3 4 5 6 7 8 9 10 11 | Enter Number of rows 5 A ABC ABCDE ABCDEFG ABCDEFGHI ABCDEFG ABCDE ABC A |
- Create a integer variable with value 64 ( we started loop with 1 so taken value 64)
- Inside loop print
System.out.print((char) (character + row));
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | import java.util.Scanner; class DimondPattern4 { public static void main(String[] args) { System.out.println("Enter Number of rows "); int character = 64; Scanner scanner = new Scanner(System.in); int rows = scanner.nextInt(); for (int row = 1; row <= rows; row++) { for (int col = 1; col <= rows - row; col++) { System.out.print(" "); } for (int col = 1; col <= 2 * row - 1; col++) { System.out.print((char) (character + row)); } System.out.println(""); } for (int row = rows - 1; row > 0; row--) { for (int col = 1; col <= rows - row; col++) { System.out.print(" "); } for (int col = 1; col <= 2 * row - 1; col++) { System.out.print((char) (character + row)); } System.out.println(""); } } } |
Output
1 2 3 4 5 6 7 8 9 10 11 | Enter Number of rows 5 A BBB CCCCC DDDDDDD EEEEEEEEE DDDDDDD CCCCC BBB A |