In this tutorial, we will write java programs to print the Diamond patterns using stars, numbers and alphabets. We have covered three examples below. In first example, we are printing diamond star pattern, in second we are printing diamond numbers pattern and in last example we are printing diamond alphabets pattern.
Example 1: Program to print Diamond Star Pattern using *

Source Code:
public class JavaExample {
public static void main(String[] args)
{
// Initializing the variable with the number at which
// the number of stars will be maximum. In this case, the
// stars will increase till 7th row, 7th row will have max
// stars and the stars will start decreasing after 7th row.
int numberOfRows = 6;
int i, j;
//This loop will print the first half of the diamond pattern
for (i = 1; i <= numberOfRows; i++) {
// This will print whitespaces, 6 spaces in first row
// 5 spaces in second row and so on
for (j = 1; j <= numberOfRows - i; j++) {
System.out.print(" ");
}
// This will print the stars after the whitespaces printed
// by above loop. It will print 1 star in first row, 3 in second
// 5 in third and so on
for (j = 1; j <= i * 2 - 1; j++) {
System.out.print("*");
}
// Move the cursor to new line after each row
System.out.println();
}
//This loop will print the second half of the diamond pattern
for (i = numberOfRows - 1; i > 0; i--) {
// This will print whitespaces in second half of triangle pattern
for (j = 1; j <= numberOfRows - i; j++) {
System.out.print(" ");
}
// This will print stars in second half of triangle pattern
for (j = 1; j <= i * 2 - 1; j++) {
System.out.print("*");
}
// Move the cursor to new line after each row
System.out.println();
}
}
}
Example 2: Program to print Diamond Pattern using numbers

Source Code:
class JavaExample {
public static void main(String[] args) {
int max = 5;
int startNumber = 1;
// First Half of the Pyramid
for (int i = 1; i <= max; i++) {
// This loop will print the whitespaces in each row
// of the first half of the Pyramid
for (int j = max; j > i; j--) {
System.out.print(" ");
}
// This loop will print the stars in each row
// of the first half of the Pyramid
for (int k = 0; k < i * 2 - 1; k++) {
System.out.print(startNumber++);
}
// Re-initializing the start number for next row
startNumber = 1;
//To move the cursor to new line for each new row
System.out.println();
}
// Second Half of the Pyramid
for (int i = 1; i <= max - 1; i++) {
// Print whitespace in second half of pattern
for (int j = 0; j < i; j++) {
System.out.print(" ");
}
// Print stars in second half of pattern
for (int k = (max - i) * 2 - 1; k > 0; k--) {
System.out.print(startNumber++);
}
// Re-initializing the start number for next row
startNumber = 1;
//new line for next row
System.out.println();
}
}
}
Example 3: Program to print Diamond Pattern using alphabets

Source Code:
class JavaExample {
public static void main(String[] args) {
//represents the max count of alphabet in the largest row
int max = 5;
//Alphanumeric value of 'A'
int alphaOfA = 65;
//This will be used to re-initialize the count
//for new row so that new row again starts with 'A'
int starTheRow = 0;
// This will print first half of the pattern
for (int i = 1; i <= max; i++) {
// Print whitespaces in each row of first half
for (int j = max; j > i; j--) {
System.out.print(" ");
}
// Print stars in each row of first half
for (int k = 0; k < i * 2 - 1; k++) {
System.out.print((char)(alphaOfA+starTheRow++));
}
//Re-initialize the count for new row
//so that new row again starts with 'A'
starTheRow = 0;
//new line for next row
System.out.println();
}
// Second half of the pattern
for (int i = 1; i <= max - 1; i++) {
// printing whitespaces in second half of pyramid
for (int j = 0; j < i; j++) {
System.out.print(" ");
}
// printing stars in second half of pyramid
for (int k = (max - i) * 2 - 1; k > 0; k--) {
System.out.print((char)(alphaOfA+starTheRow++));
}
starTheRow = 0;
//new line for next row
System.out.println();
}
}
}
Leave a Reply