Diamond Pattern in Java: Explanation & Practice
Print a diamond pattern
Problem summary
Print a diamond pattern with given width (must be odd number).
Starter code
public class Main {
public static void main(String[] args) {
int width = 5; // Test case 1 (must be odd)
// Print diamond pattern
}
}Expected output and test cases
- Width 5 diamond
* *** ***** *** *
- Width 3 diamond
* *** *
- Width 7 diamond
* *** ***** ******* ***** *** *
Hints
- First half is expanding pyramid
- Second half is shrinking pyramid
- Stars per row: 1, 3, 5... then 5, 3, 1
Related Control Flow exercises
- Practice Decimal to Binary in Java
- Practice Sum Until Condition in Java
- Practice Collatz Conjecture in Java
Practice all Control Flow exercises · Run this idea in the Java compiler