Zig-Zag Star Line

This program prints stars in zig-zag line pattern.

Problem Statement

Write a Java program that prints zig-zag star line with 3 rows and 9 columns.

Source Code

1 public class ZigZagStars {
2 public static void main(String[] args) {
3 int rows = 3, cols = 9;
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 a special case for the middle row places stars at alternating positions across rows, using modular arithmetic to create a zig-zag line effect.

Quick Links to Explore