Spiral Triangle of Alphabets

This program prints a triangular spiral of alphabets.

Problem Statement

Write a Java program that prints a triangle of 5 rows filled with alphabets in spiral order.

Source Code

1 public class SpiralTriangleAlphabets {
2 public static void main(String[] args) {
3 int n=5;
4 char ch='A';
5 for(int i=0;i<n;i++){
6 for(int j=0;j<=i;j++){
7 System.out.print(ch+" ");
8 ch++;
9 if(ch>'Z') ch='A';
10 }
11 System.out.println();
12 }
13 }
14 }

Program Output

A 
B C 
D E F 
G H I J 
K L M N O 

Explanation

A single character variable ch increments across the entire triangle row by row, resetting after 'Z', so a sequential run of alphabets fills the growing triangular shape directly.

Quick Links to Explore