Write a Java program to print triangle alphabets pattern using for loop.
package Alphabets; import java.util.Scanner; public class TriangleCharacter1 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); System.out.print("Enter Triangle Character Pattern Rows = "); int rows = sc.nextInt(); System.out.println("Printing Triangle Character/Alphabets Pattern"); for (int i = 0 ; i < rows; i++ ) { int alphabet = 65; for (int j = rows ; j > i; j-- ) { System.out.print(" "); } for (int k = 0 ; k <= i; k++ ) { System.out.print((char)(alphabet + k) + " "); } System.out.println(); } } }
This Java example displays the alphabets in a triangle pattern using a while loop.
package Alphabets; import java.util.Scanner; public class TriangleCharacter2 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); int alphabet, i = 0, j, k; System.out.print("Enter Triangle Character Pattern Rows = "); int rows = sc.nextInt(); System.out.println("Printing Triangle Character/Alphabets Pattern"); while(i < rows ) { alphabet = 65; j = rows ; while(j > i ) { System.out.print(" "); j--; } k = 0 ; while( k <= i ) { System.out.print((char)(alphabet + k) + " "); k++; } System.out.println(); i++; } } }
Enter Triangle Character Pattern Rows = 9
Printing Triangle Character/Alphabets Pattern
A
A B
A B C
A B C D
A B C D E
A B C D E F
A B C D E F G
A B C D E F G H
A B C D E F G H I
Java program to print triangle alphabets pattern using do while loop.
package Alphabets; import java.util.Scanner; public class TriangleCharacter3 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); int alphabet, i = 0, j, k; System.out.print("Enter Triangle Character Pattern Rows = "); int rows = sc.nextInt(); System.out.println("Printing Triangle Character/Alphabets Pattern"); do { alphabet = 65; j = rows ; do { System.out.print(" "); } while(--j > i ); k = 0 ; do { System.out.print((char)(alphabet + k) + " "); } while( ++k <= i ); System.out.println(); } while(++i < rows ); } }
Enter Triangle Character Pattern Rows = 14
Printing Triangle Character/Alphabets Pattern
A
A B
A B C
A B C D
A B C D E
A B C D E F
A B C D E F G
A B C D E F G H
A B C D E F G H I
A B C D E F G H I J
A B C D E F G H I J K
A B C D E F G H I J K L
A B C D E F G H I J K L M
A B C D E F G H I J K L M N