Star Zig-Zag Wave

This program prints stars in zig-zag wave form.

Problem Statement

Write a Java program that prints zig-zag wave of stars with 3 rows and 15 columns.

Source Code

1 public class StarZigZagWave {
2 public static void main(String[] args) {
3 int rows = 3, cols = 15;
4 for(int i=1; i<=rows; i++) {
5 for(int j=1; j<=cols; j++) {
6 if((i+j)%4==0 || (i==2 && j%4==0)) System.out.print("*");
7 else System.out.print(" ");
8 }
9 System.out.println();
10 }
11 }
12 }

Program Output

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

Explanation

The condition (i+j)%4==0, combined with the middle-row special case, places stars in a repeating wave pattern across 15 columns, showing how modular arithmetic aligns a wider zig-zag wave.

Quick Links to Explore