Diamond Program in Java – Both Full & Half Diamond [2023]

In this tutorial, we will learn about the Diamond program in Java. We have given the example of both full and half diamond patterns.

What is Diamond Pattern in Java?

A star pattern that is represented in a diamond manner is called a Diamond Pattern and to develop a program to represent this diamond pattern is called a diamond pattern program.

Example:

Diamond Pattern Program in Java

Also Read: Java Program To Print Vowels In A String – 2 Simple Programs

Simple Logic Behind Full Diamond Program in Java

First, we will learn the logic to draw a diamond pattern in Java.

As we have to display a diamond pattern, so we have to display two halves of a triangle(upper half & lower half) at a time.

We will ask the User to accept the no of lines of the diamond pattern he/she wants to display. Once we will accept the no of lines, we can use this no of lines to display the Diamond pattern further.

We have used 2 different for loops for both the upper half & lower halves. For the first upper half, we have to print both space and stars simultaneously. So we have used two different inner for loops one for space and another for stars at a time.

Similarly for the lower half also we have used two different inner for loops to print both space and star simultaneously. For the first upper half, the no of spaces will be in decreasing manner and whereas in the lower half, spaces will be printed in an increasing manner.

Let’s see the complete program now.

Full Diamond Program in Java

/**
 * Java Program to Print Full Diamond Pattern in Java.
 */
package cf.java.pattern;

import java.util.Scanner;

public class FullDiamondPattern {

	public static void main(String[] args) {

		int noOfLines, row, col, noOfspace = 1;
		System.out.print("Enter the number of rows: ");
		Scanner scanner = new Scanner(System.in);
		noOfLines = scanner.nextInt();
		noOfspace = noOfLines - 1;
		System.out.println("Full Diamond Pattern:");
		for (col = 1; col <= noOfLines; col++) {
			for (row = 1; row <= noOfspace; row++) {
				System.out.print(" ");
			}
			noOfspace--;
			for (row = 1; row <= 2 * col - 1; row++) {
				System.out.print("*");
			}
			System.out.println("");
		}
		noOfspace = 1;
		for (col = 1; col <= noOfLines - 1; col++) {
			for (row = 1; row <= noOfspace; row++) {
				System.out.print(" ");
			}
			noOfspace++;
			for (row = 1; row <= 2 * (noOfLines - col) - 1; row++) {
				System.out.print("*");
			}
			System.out.println("");
		}
	}

}Code language: Java (java)

Output

Enter the number of rows: 5
Full Diamond Pattern:
    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *

Also Read: How to convert List to Byte Array in Java? – Easy way

Simple Logic Behind Upper Half Diamond Program in Java

As we have already seen in the above program that how can we print a full diamond pattern in Java which is also covered this upper half diamond pattern.

So first of all, we will ask the user to provide the no of lines of the half diamond pattern he/she wants to display. Once we will accept the no of lines, we can use this no of lines to display the half Diamond pattern further.

Here we will use one outer for loop and 2 inner for loops to print the half diamond pattern.

As we have already explained in the above program, for the upper half, we have to print both space and stars simultaneously. So we have used two different inner for loops one for space and another for stars at a time and will decrease the no of spaces by one on each iteration.

Let’s see the complete program to print the upper half diamond pattern.

Upper Half Diamond Program in Java

/**
 * Upper Half Diamond Program in Java.
 */
package cf.java.pattern;

import java.util.Scanner;

public class FullDiamondPattern {

	public static void main(String[] args) {

		int noOfLines, row, col, noOfspace = 1;
		System.out.print("Enter the number of rows: ");
		Scanner scanner = new Scanner(System.in);
		noOfLines = scanner.nextInt();
		noOfspace = noOfLines - 1;
		System.out.println("Full Diamond Pattern:");
		for (col = 1; col <= noOfLines; col++) {
			for (row = 1; row <= noOfspace; row++) {
				System.out.print(" ");
			}
			noOfspace--;
			for (row = 1; row <= 2 * col - 1; row++) {
				System.out.print("*");
			}
			System.out.println("");
		}
		
	}

}Code language: Java (java)

Output

Enter the number of rows: 5
Full Diamond Pattern:
    *
   ***
  *****
 *******
*********

Simple Logic Behind Lower Half Diamond Program

As we have already seen in the first program that how can we print a full diamond pattern in Java which is also covered this lower half diamond pattern.

So first of all, we will ask the user to provide the no of lines of the lower half diamond pattern he/she wants to display. Once we will accept the no of lines, we can use this no of lines to display the lower half Diamond pattern further.

Here we will use one outer for loop and 2 inner for loops to print the lower half diamond pattern.

As we have already explained in the above program, for the lower half also we have used two different inner for loops to print both space and star simultaneously. For the lower half, spaces will be printed in an increasing manner.

Let’s see the complete program to print the lower half diamond pattern.

Lower Half Diamond Program in Java

/**
 * Lower Half Diamond Program in Java.
 */
package cf.java.pattern;

import java.util.Scanner;

public class FullDiamondPattern {

	public static void main(String[] args) {

		int noOfLines, row, col, noOfspace = 1;
		System.out.print("Enter the number of rows: ");
		Scanner scanner = new Scanner(System.in);
		noOfLines = scanner.nextInt();
		noOfspace = 1;
                System.out.println("Full Diamond Pattern:");
		for (col = 1; col <= noOfLines - 1; col++) {
			for (row = 1; row <= noOfspace; row++) {
				System.out.print(" ");
			}
			noOfspace++;
			for (row = 1; row <= 2 * (noOfLines - col) - 1; row++) {
				System.out.print("*");
			}
			System.out.println("");
		}
	}

}Code language: Java (java)

Output

Enter the number of rows: 5
Full Diamond Pattern:
 *******
  *****
   ***
    *

Other Queries We Have Covered

  • Java Program to Print Diamond Pattern.
  • How to Display Diamond Pattern in Java?
  • How to Print Diamond Pattern in Java?
  • How to Print Diamond Star Pattern in Java?

FAQs

What is Diamond Pattern?

A star pattern that is represented in a diamond manner is called a diamond pattern.

What is Inner for Loop?

One for loop which is declared inside another for loop is called as inner for loop.

What is Outer for loop?

One for loop which contains one or more inner loops is called the outer for loop.

Leave a Comment