Java Program to Print Diamond Pattern
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
How to Think About It
Algorithm
Code
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(); } }
Dry Run
Let's trace input 3 through the code
Input read
n = 3
Print upper half
i=1: spaces=2, stars=1 -> ' *' i=2: spaces=1, stars=3 -> ' ***' i=3: spaces=0, stars=5 -> '*****'
Print lower half
i=2: spaces=1, stars=3 -> ' ***' i=1: spaces=2, stars=1 -> ' *'
| i | Spaces | Stars | Line Output |
|---|---|---|---|
| 1 | 2 | 1 | * |
| 2 | 1 | 3 | *** |
| 3 | 0 | 5 | ***** |
| 2 | 1 | 3 | *** |
| 1 | 2 | 1 | * |
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
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(); } }
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); } }
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.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Two nested loops | O(n^2) | O(1) | Clarity and simplicity |
| Single loop with condition | O(n^2) | O(1) | Concise code |
| Recursion | O(n^2) | O(n) due to call stack | Learning recursion concepts |