Concentric Hexagon Hybrid
This program prints concentric hexagons alternating stars and numbers.
Problem Statement
Write a Java program that prints concentric hexagons where odd rows have stars and even rows have numbers.
Source Code
| 1 | public class ConcentricHexagonHybrid { |
| 2 | public static void main(String[] args) { |
| 3 | int n = 6; |
| 4 | for(int i=1; i<=n; i++) { |
| 5 | for(int j=i; j<n; j++) System.out.print(" "); |
| 6 | for(int j=1; j<=n; j++) { |
| 7 | if(i%2==1) System.out.print("* "); |
| 8 | else System.out.print(i + " "); |
| 9 | } |
| 10 | System.out.println(); |
| 11 | } |
| 12 | } |
| 13 | } |
Program Output
* * * * * *
2 2 2 2 2 2
* * * * * *
4 4 4 4 4 4
* * * * * *
6 6 6 6 6 6
Explanation
The condition i%2==1 decides whether each full row prints stars or the row's own repeated number, so rows alternate between two symbol types forming a hexagon hybrid shape.