Zig-Zag Alphabet Wave
This program prints alphabets in zig-zag wave form.
Problem Statement
Write a Java program that prints zig-zag alphabet wave with 3 rows and 15 columns.
Source Code
| 1 | public class ZigZagAlphabetWave { |
| 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 | 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
Explanation
The same zig-zag position condition as the star wave is used, but instead of a fixed star it prints and increments a rotating alphabet character, so the wave is filled with sequential letters instead of stars.