Diagonal Star-Alphabet Hybrid
This program prints stars with alphabets on diagonals.
Problem Statement
Write a Java program that prints a 7x7 grid of stars with alphabets placed on diagonals.
Source Code
| 1 | public class DiagonalStarAlphabetHybrid { |
| 2 | public static void main(String[] args) { |
| 3 | int n = 7; |
| 4 | for(int i=0; i<n; i++) { |
| 5 | for(int j=0; j<n; j++) { |
| 6 | if(i==j || i+j==n-1) System.out.print((char)('A'+i) + " "); |
| 7 | else System.out.print("* "); |
| 8 | } |
| 9 | System.out.println(); |
| 10 | } |
| 11 | } |
| 12 | } |
Program Output
A * * * * * G * B * * * F * * * C * E * * * * * D * * * * * E * C * * * F * * * B * G * * * * * A
Explanation
The diagonal condition i==j || i+j==n-1 prints an alphabet derived from the row index on both diagonals, while every other cell is filled with a star, forming an X-shaped alphabet overlay on a star grid.