BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java Program to Print Diamond Pattern

Last Updated: June 24, 2022 by Chaitanya Singh | Filed Under: Java Examples

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 *

Print Diamond Star Pattern in Java

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

Print Pyramid star pattern of 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

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();
    }
  }
}

Related Java Programs

  1. Java Program to print Pyramid Star Pattern
  2. Java Program to print left triangle star Pattern
  3. Java Program to print Right triangle Star Pattern
❮ Learn JavaJava Examples ❯

Top Related Articles:

  1. Java Program to Print Left Pascal Triangle Pattern
  2. Program to Implement Merge Sort in Java
  3. Java Program to remove duplicate elements in an Array
  4. Java Program to Print Right Pascal’s Triangle Pattern
  5. Java Program to Print Downward Triangle Star Pattern

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Examples

  • Check Odd-even
  • Linear Search
  • Binary Search
  • Floyd's Triangle
  • Reverse number
  • Random Number
  • first n prime numbers
  • Disp prime Numbers
  • Check Prime number
  • Palindrome String
  • Find factorial
  • Sum of elements of Array
  • Area of rectangle
  • Area of Square
  • Area of Triangle
  • Circle

Tutorials

  • Java Tutorial
  • OOPs Concepts
  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap