Write a Java Program to Print Alphabets from A to Z using for loop. In this example, for loop will iterate from alphabets A to Z and print them as an output.
public class AlphabetsAtoZ { public static void main(String[] args) { char ch; for(ch = 'A'; ch <= 'Z'; ch++) { System.out.print(ch + " "); } } }
This Java program iterates the ASCII codes from 65 to 90, representing the alphabets A to Z, and prints them. Here, we used for loop, while loop, and do while loop to show you the possibilities.
public class AlphabetsAtoZ2 { public static void main(String[] args) { for(int i = 65; i <= 90; i++) { System.out.printf("%c ", i); } System.out.println(); int j = 65; while(j <= 90) { System.out.printf("%c ", j); j++; } System.out.println(); int k = 65; do { System.out.printf("%c ", k); } while(++k <= 90); } }
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
This program allows the user to enter the starting alphabet A and print the remaining uppercase alphabets up to Z.
import java.util.Scanner; public class AlphabetsAtoZ3 { private static Scanner sc; public static void main(String[] args) { char ch, strChar; sc= new Scanner(System.in); System.out.print("Please Enter any Character = "); strChar = sc.next().charAt(0); for(ch = strChar; ch <= 'Z'; ch++) { System.out.print(ch + " "); } } }
Please Enter any Character = G
G H I J K L M N O P Q R S T U V W X Y Z