[Java] Diamond Pattern Java Program

In this “Diamond Pattern” – We have written Java programs to print/draw Star (X) pattern by using different types of Java loops and you can execute and try these Java program through your IDE. This is most [100+] Frequently Asked Java Program on beginner level to check you Java Programming skills and Concept.

Logic

In the Diamond Pattern following this logic,

  • Diamond will draw in two phases upper part and lower part (just opposite logic)
  • First and last row will have only one symbol.
  • Consider spaces from left before print symbols
  • Once last symbol print jump cursor to next row (n)
Diamond Pattern

This post covers following ways to print Diamond Pattern:

Diamond Pattern Java Program: For Loop

This Diamond Java Program is following the below for loop logic:

package _1_patterns.diamond;

import java.util.Scanner;

public class DiamondPatterForLoop {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		System.out.println("Enter Pattern Size : ");
		int size = sc.nextInt();

		System.out.print("Enter Symbol : ");
		char symbol = sc.next().charAt(0);

		drawDiamondPattern(size, symbol);

		// Close Scanner
		sc.close();
	}

	private static void drawDiamondPattern(int n, char c) {

		// prints upper section of the pattern
		for (int i = 1; i <= n; i++) {
			// print spaces from left
			for (int j = n; j > i; j--) {
				System.out.print(" ");
			}
			// print symbol in diamond
			for (int j = 0; j < (i - 1) * 2; j++) {
				System.out.print(c);
			}
			if (i == 0) {
				// throws cursor to the next line as first row will have one symbol
				System.out.print("\n");
			} else {
				// prints symbol and throws cursor to the next line
				System.out.print(c + "\n");
			}
		}
		// prints lower section of the pattern
		for (int i = n - 1; i >= 1; i--) {
			// print spaces from left
			for (int j = n; j > i; j--) {
				System.out.print(" ");
			}
			// print symbol of  diamond
			for (int j = 0; j < (i - 1) * 2; j++) {
				System.out.print(c);
			}
			if (i == 0) {
				// throws cursor to the next line because last row will have one symbol
				System.out.print("\n");
			} else {
				// prints symbol and throws cursor to the next line
				System.out.print(c + "\n");
			}
		}
	}
}

Output

Enter Pattern Size : 
8
Enter Symbol : X
       X
      XXX
     XXXXX
    XXXXXXX
   XXXXXXXXX
  XXXXXXXXXXX
 XXXXXXXXXXXXX
XXXXXXXXXXXXXXX
 XXXXXXXXXXXXX
  XXXXXXXXXXX
   XXXXXXXXX
    XXXXXXX
     XXXXX
      XXX
       X

Diamond Pattern Java Program: While Loop

This Diamond Java Program is following the below while loop logic:

package _1_patterns.diamond;

import java.util.Scanner;

public class DiamondPatterWhileLoop {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		System.out.println("Enter Pattern Size : ");
		int size = sc.nextInt();

		System.out.print("Enter Symbol : ");
		char symbol = sc.next().charAt(0);

		drawDiamondPattern(size, symbol);

		// Close Scanner
		sc.close();
	}

	private static void drawDiamondPattern(int n, char c) {

		// prints upper section of the pattern
		int i = 0;
		while (i <= n) {
			// print spaces from left
			int j = n;
			while (j > i) {
				System.out.print(" ");
				j--;
			}
			// print symbol in diamond
			j = 0;
			while (j < (i - 1) * 2) {
				System.out.print(c);
				j++;
			}
			if (i == 0) {
				// throws cursor to the next line as first row will have one symbol
				System.out.print("\n");
			} else {
				// prints symbol and throws cursor to the next line
				System.out.print(c + "\n");
			}
			i++;
		}
		// prints lower section of the pattern
		i = n - 1;
		while (i >= 1) {
			// print spaces from left
			int j = n;
			while (j > i) {
				System.out.print(" ");
				j--;
			}
			// print symbol of diamond
			j = 0;
			while (j < (i - 1) * 2) {
				System.out.print(c);
				j++;
			}
			if (i == 0) {
				// throws cursor to the next line because last row will have one symbol
				System.out.print("\n");
			} else {
				// prints symbol and throws cursor to the next line
				System.out.print(c + "\n");
			}
			i--;
		}
	}
}

Output

Enter Pattern Size : 
8
Enter Symbol : $
       $
      $$$
     $$$$$
    $$$$$$$
   $$$$$$$$$
  $$$$$$$$$$$
 $$$$$$$$$$$$$
$$$$$$$$$$$$$$$
 $$$$$$$$$$$$$
  $$$$$$$$$$$
   $$$$$$$$$
    $$$$$$$
     $$$$$
      $$$
       $

Diamond Pattern Java Program: Do-While Loop

This Diamond Java Program is following the below do-while loop logic:

package _1_patterns.diamond;

import java.util.Scanner;

public class DiamondPatterDoWhileLoop {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		System.out.println("Enter Pattern Size : ");
		int size = sc.nextInt();

		System.out.print("Enter Symbol : ");
		char symbol = sc.next().charAt(0);

		drawDiamondPattern(size, symbol);

		// Close Scanner
		sc.close();
	}

	private static void drawDiamondPattern(int n, char c) {

		// prints upper section of the pattern
		int i = 0;
		do {
			// print spaces from left
			int j = n;
			do {
				System.out.print(" ");
				j--;
			} while (j > i);
			// print symbol in diamond
			j = 0;
			do {
				System.out.print(c);
				j++;
			} while (j < (i - 1) * 2);
			if (i == 0) {
				// throws cursor to the next line as first row will have one symbol
				System.out.print("\n");
			} else {
				// prints symbol and throws cursor to the next line
				System.out.print(c + "\n");
			}
			i++;
		} while (i <= n);
		// prints lower section of the pattern
		i = n - 1;
		do {
			// print spaces from left
			int j = n;
			do {
				System.out.print(" ");
				j--;
			} while (j > i);
			// print symbol of diamond
			j = 0;
			do {
				System.out.print(c);
				j++;
			} while (j < (i - 1) * 2);
			if (i == 0) {
				// throws cursor to the next line because last row will have one symbol
				System.out.print("\n");
			} else {
				// prints symbol and throws cursor to the next line
				System.out.print(c + "\n");
			}
			i--;
		} while (i >= 1);
	}
}

Output

Enter Pattern Size : 
8
Enter Symbol : #
       #
      ###
     #####
    #######
   #########
  ###########
 #############
###############
 #############
  ###########
   #########
    #######
     #####
      ###
       #

Related Posts

Your Feedback Motivate Us

If our FacingIssuesOnIT Experts solutions guide you to resolve your issues and improve your knowledge. Please share your comments, like and subscribe to get notifications for our posts.

Happy Learning !!!