Spiral Triangle Hybrid (Numbers + Alphabets)

This program prints a triangular spiral alternating numbers and alphabets.

Problem Statement

Write a Java program that prints a triangle of 6 rows where spiral order alternates numbers and alphabets.

Source Code

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

Program Output

1 
A 2 
B 3 C 
4 D 5 E 
F 6 G 7 H 
I 8 J 9 K 10 

Explanation

A boolean toggle alternates between printing a number and an alphabet for each successive cell across the whole triangle, so adjacent positions consistently alternate between the two symbol types.

Quick Links to Explore