Hollow Concentric Pentagon Hybrid
This program prints hollow concentric pentagons alternating stars and alphabets.
Problem Statement
Write a Java program that prints concentric hollow pentagons where odd rows have stars and even rows have alphabets.
Source Code
| 1 | public class HollowConcentricPentagonHybrid { |
| 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<=2*i+1; j++) { |
| 7 | if(j==1 || j==2*i+1 || i==n) { |
| 8 | if(i%2==1) System.out.print("*"); |
| 9 | else System.out.print((char)('A'+i-1)); |
| 10 | } else System.out.print(" "); |
| 11 | } |
| 12 | System.out.println(); |
| 13 | } |
| 14 | } |
| 15 | } |
Program Output
*
B B
* *
D D
* *
FFFFFFFFFFF
Explanation
The widened hollow pentagon boundary is combined with a row-parity check, so odd rows print stars at the boundaries while even rows print a letter derived from the row index, forming a hybrid pentagon.