Advanced Numeric-Alphabet Pyramid with Stars

This program prints a pyramid alternating numbers, alphabets, and stars.

Problem Statement

Write a Java program that prints a pyramid of 6 rows alternating numbers, alphabets, and stars cyclically.

Source Code

1 public class AdvancedNumericAlphabetPyramidStars {
2 public static void main(String[] args) {
3 int n = 6, num=1;
4 char ch='A';
5 for(int i=1; i<=n; i++) {
6 for(int j=i; j<n; j++) System.out.print(" ");
7 for(int j=1; j<=2*i-1; j++) {
8 int mod=j%3;
9 if(mod==1) System.out.print(num++ + " ");
10 else if(mod==2) {
11 System.out.print(ch + " ");
12 ch++;
13 if(ch>'Z') ch='A';
14 } else System.out.print("* ");
15 }
16 System.out.println();
17 }
18 }
19 }

Program Output

     1      
    2 A *    
   3 B * 4 C *  
  5 D * 6 E * 7 F * 
 8 G * 9 H * 10 I * 11 J * 
12 K * 13 L * 14 M * 15 N * 16 O * 

Explanation

Within each row, the position j%3 cycles through printing a number, an alphabet, or a star, so the pyramid rows themselves show a repeating three-symbol cyclic sequence rather than switching per row.

Quick Links to Explore