Alphabet Triangle
This program prints an alphabet triangle.
Problem Statement
Write a Java program that prints an alphabet triangle with 5 rows.
Source Code
| 1 | public class AlphabetTriangle { |
| 2 | public static void main(String[] args) { |
| 3 | int n = 5; |
| 4 | for(int i=1; i<=n; i++) { |
| 5 | char ch = 'A'; |
| 6 | for(int j=1; j<=i; j++) { |
| 7 | System.out.print(ch + " "); |
| 8 | ch++; |
| 9 | } |
| 10 | System.out.println(); |
| 11 | } |
| 12 | } |
| 13 | } |
Program Output
A A B A B C A B C D A B C D E
Explanation
Each row resets ch to 'A' and the inner loop prints alphabets starting from 'A', incrementing ch each time, so each row prints more alphabets than the previous one, demonstrating character manipulation in loops.