0
0
JavaProgramBeginner · 2 min read

Java Program to Print Diamond Pattern

You can print a diamond pattern in Java by using nested for loops to print spaces and stars in the correct order, like for (int i = 1; i <= n; i++) { for (int j = n - i; j > 0; j--) System.out.print(" "); for (int k = 1; k <= 2 * i - 1; k++) System.out.print("*"); System.out.println(); } for the upper half and a similar loop for the lower half.
📋

Examples

Input5
Output * *** ***** ******* ********* ******* ***** *** *
Input3
Output * *** ***** *** *
Input1
Output*
🧠

How to Think About It

To print a diamond pattern, first print the top half by increasing the number of stars each line and decreasing spaces before them. Then print the bottom half by decreasing stars and increasing spaces. Use loops to control spaces and stars for each line.
📐

Algorithm

1
Get input number n for diamond size
2
For i from 1 to n, print (n - i) spaces and (2 * i - 1) stars
3
For i from n-1 down to 1, print (n - i) spaces and (2 * i - 1) stars
4
End
💻

Code

java
import java.util.Scanner;
public class DiamondPattern {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        for (int i = 1; i <= n; i++) {
            for (int j = n - i; j > 0; j--) System.out.print(" ");
            for (int k = 1; k <= 2 * i - 1; k++) System.out.print("*");
            System.out.println();
        }
        for (int i = n - 1; i >= 1; i--) {
            for (int j = n - i; j > 0; j--) System.out.print(" ");
            for (int k = 1; k <= 2 * i - 1; k++) System.out.print("*");
            System.out.println();
        }
        sc.close();
    }
}
Output
* *** ***** ******* ********* ******* ***** *** *
🔍

Dry Run

Let's trace input 3 through the code

1

Input read

n = 3

2

Print upper half

i=1: spaces=2, stars=1 -> ' *' i=2: spaces=1, stars=3 -> ' ***' i=3: spaces=0, stars=5 -> '*****'

3

Print lower half

i=2: spaces=1, stars=3 -> ' ***' i=1: spaces=2, stars=1 -> ' *'

iSpacesStarsLine Output
121 *
213 ***
305*****
213 ***
121 *
💡

Why This Works

Step 1: Upper half printing

The first loop prints the top half of the diamond by printing spaces first, then stars, increasing stars by 2 each line.

Step 2: Lower half printing

The second loop prints the bottom half by reversing the process, decreasing stars and increasing spaces.

Step 3: Nested loops for alignment

Nested loops control spaces and stars separately to align the diamond shape properly.

🔄

Alternative Approaches

Using single loop with condition
java
import java.util.Scanner;
public class DiamondPatternAlt {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int totalLines = 2 * n - 1;
        for (int i = 1; i <= totalLines; i++) {
            int stars = i <= n ? 2 * i - 1 : 2 * (totalLines - i + 1) - 1;
            int spaces = (2 * n - 1 - stars) / 2;
            for (int j = 0; j < spaces; j++) System.out.print(" ");
            for (int k = 0; k < stars; k++) System.out.print("*");
            System.out.println();
        }
        sc.close();
    }
}
This method uses one loop and calculates stars and spaces based on line number, making code shorter but slightly more complex.
Using recursion
java
public class DiamondPatternRec {
    public static void printDiamond(int n, int i) {
        if (i > n) return;
        for (int j = n - i; j > 0; j--) System.out.print(" ");
        for (int k = 1; k <= 2 * i - 1; k++) System.out.print("*");
        System.out.println();
        printDiamond(n, i + 1);
        if (i != n) {
            for (int j = n - i; j > 0; j--) System.out.print(" ");
            for (int k = 1; k <= 2 * i - 1; k++) System.out.print("*");
            System.out.println();
        }
    }
    public static void main(String[] args) {
        printDiamond(3, 1);
    }
}
Recursion prints the diamond by calling itself for the upper half and then printing the lower half on return, but is less intuitive for beginners.

Complexity: O(n^2) time, O(1) space

Time Complexity

The program uses nested loops where the outer loop runs about 2n times and inner loops run up to 2n times, resulting in O(n^2) time.

Space Complexity

Only a few variables are used; no extra data structures, so space complexity is O(1).

Which Approach is Fastest?

All approaches have similar time complexity; the single loop method is more concise but slightly harder to read.

ApproachTimeSpaceBest For
Two nested loopsO(n^2)O(1)Clarity and simplicity
Single loop with conditionO(n^2)O(1)Concise code
RecursionO(n^2)O(n) due to call stackLearning recursion concepts
💡
Use nested loops: one for spaces and one for stars to align the diamond shape.
⚠️
Beginners often forget to print the lower half of the diamond or miscalculate spaces causing misalignment.