Star-Alphabet Hybrid Pyramid
This program prints a pyramid combining stars and alphabets.
Problem Statement
Write a Java program that prints a pyramid where odd rows have stars and even rows have alphabets.
Source Code
| 1 | public class StarAlphabetHybrid { |
| 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 k=1; k<=2*i-1; k++) { |
| 7 | if(i%2==1) System.out.print("*"); |
| 8 | else System.out.print((char)('A'+k-1)); |
| 9 | } |
| 10 | System.out.println(); |
| 11 | } |
| 12 | } |
| 13 | } |
Program Output
*
ABC
*****
ABCDE
*********
ABCDEFGH
Explanation
The condition i%2==1 decides whether the current row prints stars or a sequence of alphabets, so odd and even rows alternate between two symbol types, forming a hybrid pyramid.