Advanced Zig-Zag Alphabet Wave

This program prints an advanced zig-zag alphabet wave with alternating direction.

Problem Statement

Write a Java program that prints zig-zag alphabet wave with 4 rows and 20 columns, alternating direction each row.

Source Code

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

Program Output

   A   B   C   D   E   
  F G H I J K L M N O  
 P   Q   R   S   T   U 
 V W X Y Z A B C D E F 

Explanation

Additional row-specific conditions are added to the standard zig-zag rule, so different rows have different densities of printed alphabets, producing a more advanced and varied wave pattern.

Quick Links to Explore