Write a Java program to print K shape alphabets pattern using for loop.
package Alphabets; import java.util.Scanner; public class kShapeAlphabets1 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); int i, j, alphabet; System.out.print("Enter K Shape Alphabets Pattern Rows = "); int rows = sc.nextInt(); System.out.println("Printing K Shape Alphabets Pattern"); for (i = rows - 1; i >= 0; i-- ) { alphabet = 65; for (j = 0 ; j <= i; j++ ) { System.out.print((char)(alphabet + j) + " "); } System.out.println(); } for (i = 1 ; i < rows; i++ ) { alphabet = 65; for (j = 0 ; j <= i; j++ ) { System.out.print((char)(alphabet + j) + " "); } System.out.println(); } } }
This Java example displays the alphabets arranged in K shape pattern using a while loop.
package Alphabets; import java.util.Scanner; public class kShapeAlphabets2 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); System.out.print("Enter K Shape Alphabets Pattern Rows = "); int rows = sc.nextInt(); System.out.println("Printing K Shape Alphabets Pattern"); int i = rows - 1, j, alphabet; while (i >= 0 ) { alphabet = 65; j = 0 ; while ( j <= i) { System.out.print((char)(alphabet + j) + " "); j++; } System.out.println(); i--; } i = 1 ; while ( i < rows ) { alphabet = 65; j = 0 ; while ( j <= i ) { System.out.print((char)(alphabet + j) + " "); j++; } System.out.println(); i++; } } }
Enter K Shape Alphabets Pattern Rows = 7
Printing K Shape Alphabets Pattern
A B C D E F G
A B C D E F
A B C D E
A B C D
A B C
A B
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
Java program to print K shape alphabets pattern using do while loop.
package Alphabets; import java.util.Scanner; public class kShapeAlphabets3 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); System.out.print("Enter K Shape Alphabets Pattern Rows = "); int rows = sc.nextInt(); System.out.println("Printing K Shape Alphabets Pattern"); int i = rows - 1, j, alphabet; do { alphabet = 65; j = 0 ; do { System.out.print((char)(alphabet + j) + " "); } while ( ++j <= i); System.out.println(); } while (--i >= 0 ); i = 1 ; do { alphabet = 65; j = 0 ; do { System.out.print((char)(alphabet + j) + " "); } while ( ++j <= i ); System.out.println(); } while ( ++i < rows ); } }
Enter K Shape Alphabets Pattern Rows = 10
Printing K Shape Alphabets Pattern
A B C D E F G H I J
A B C D E F G H I
A B C D E F G H
A B C D E F G
A B C D E F
A B C D E
A B C D
A B C
A B
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