Print Diamond Shape in Java



In this article, we will learn how to print a diamond shape pattern using nested loops in Java. This helps in understanding loops and conditional structures in Java programming.

A nested loop refers to a loop placed within another loop. This structure is often called "loops within loops" because the inner loop runs completely each time the outer loop iterates.

Diamond Shape

A diamond shape is a symmetrical pattern that consists of two parts: an upper triangle and a mirrored inverted triangle below it. In programming, such patterns are typically printed using nested loops to control spaces and symbols.

A diamond shape can be printed by printing a triangle and then an inverted triangle.

An example of this is given as follows ?

*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*

Printing a diamond shape

The following are the steps to print a diamond shape using Java program:

Step 1: Initialize variables

  • Initialize n as the number of rows for the top half of the diamond.
  • Set s to n - 1 to manage spacing.
Step 2: Print the top half of the diamond
  • Loop from 0 to n-1 for each row.
  • For each row, print s spaces (decrease s by 1 after each row).
  • Print stars (*) with a space after each.

The code snippet for the upper triangle is given as follows ?

int n = 6;
int s = n - 1;
System.out.print("A diamond with " + 2 * n + " rows is as follows:\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < s; j++)
		System.out.print(" ");
	for (int j = 0; j <= i; j++)
		System.out.print("* ");
	System.out.print("\n");
	                 s--;
}
Step 3: Print the bottom half of the diamond
  • Set s back to 0 to manage spacing for the bottom part.
  • Loop from n to 1 for the rows.
  • For each row, print s spaces (increase s by 1 after each row).
  • Print stars (*) with a space after each.

The code snippet for the inverted lower triangle is given as follows ?

s = 0;
for (int i = n; i > 0; i--) {
	for (int j = 0; j < s; j++)
		System.out.print(" ");
	for (int j = 0; j < i; j++)
		System.out.print("* ");
	System.out.print("\n");
	                 s++;
}

Java program to print the diamond-shape

The program that demonstrates this is given as follows:

public class Example {
	public static void main(String[] args) {
		int n = 6;
		int s = n - 1;
		System.out.print("A diamond with " + 2 * n + " rows is as follows:\n");

		for (int i = 0; i < n; i++) {
			for (int j = 0; j < s; j++)
				System.out.print(" ");
			for (int j = 0; j <= i; j++)
				System.out.print("* ");
			System.out.print("\n");
			s--;
		}

		s = 0;
		for (int i = n; i > 0; i--) {
			for (int j = 0; j < s; j++)
				System.out.print(" ");
			for (int j = 0; j < i; j++)
				System.out.print("* ");
			System.out.print("\n");
			s++;
		}
	}
}

Output

A diamond with 12 rows is as follows:

*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
Updated on: 2024-11-23T03:55:38+05:30

256 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements