Advanced Numeric-Alphabet Pyramid

This program prints a pyramid alternating numbers and alphabets in each row.

Problem Statement

Write a Java program that prints a pyramid of 6 rows where each row alternates numbers and alphabets.

Source Code

1 public class AdvancedNumericAlphabetPyramid {
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 if(j%2==1) System.out.print(num++ + " ");
9 else {
10 System.out.print(ch + " ");
11 ch++;
12 if(ch>'Z') ch='A';
13 }
14 }
15 System.out.println();
16 }
17 }
18 }

Program Output

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

Explanation

Instead of switching symbol type per row, the position within the row itself (j%2) decides whether a number or an alphabet is printed, so numbers and alphabets alternate horizontally in each row of the pyramid.

Quick Links to Explore