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

  1. First half is expanding pyramid
  2. Second half is shrinking pyramid
  3. Stars per row: 1, 3, 5... then 5, 3, 1

Related Control Flow exercises

Practice all Control Flow exercises · Run this idea in the Java compiler