Zig-Zag Alphabet Wave with Stars

This program prints alphabets in zig-zag wave form with stars at intersections.

Problem Statement

Write a Java program that prints zig-zag alphabet wave with stars marking intersections.

Source Code

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

Program Output

   A   *   C   *   
  E * G * I * K *  
 M   *   O   *   Q 

Explanation

The zig-zag wave positions are computed as before, but the character's underlying value determines whether a star or the alphabet itself is printed, creating a hybrid pattern of characters and stars along the wave.

Quick Links to Explore