Java Program to Print Alphabets from A to Z Using Loop
The main aim of the title is to print the English alphabet from A to Z using a loop in Java, we can utilize the loop structure. Initialize a char variable to 'A', set the termination condition as less than or equal to 'Z', and increment the variable in each iteration to print the alphabet sequentially
![Java Program to Print Alphabets from A to Z Using Loop/>
<!-- /wp:html -->
<!-- wp:paragraph -->
<p>There are different methods are available to print the alphabets from A to Z and a to z.</p>
<!-- /wp:paragraph -->
<!-- wp:list {](https://static.tutorialandexample.com/java/java-program-to-print-alphabets-from-a-to-z-using-loop1.png)
Method 1: Using ASCII Values
ALGORITHM:
Step 1: Iterate over the ASCII values for uppercase letters, starting from 65 ('A') and ending at 90 ('Z'). The range corresponds to the ASCII values of the uppercase alphabet.
Step 2: Cast each integer ASCII value to a char data type using an explicit type cast (char) to convert it to the corresponding character.
Step 3: Print the character obtained from the ASCII value using System.out.print(), followed by a space for better readability.
Step 4: Repeat the process for lowercase letters, iterating over the ASCII values from 97 ('a') to 122 ('z'). The range corresponds to the ASCII values of the lowercase alphabet.
Implementation:
The implementation of the above steps given below
FileName: PrintAlphabets1.java
import java.util.*; public class PrintAlphabets1 { public static void main(String[] args) { // Printing uppercase alphabets using ASCII values System.out.println("Uppercase Alphabets:"); for (int i = 65; i <= 90; i++) { char ch = (char) i; System.out.print(ch + " "); } System.out.println(" Lowercase Alphabets:"); // Printing lowercase alphabets using ASCII values for (int i = 97; i <= 122; i++) { char ch = (char) i; System.out.print(ch + " "); } } }
Output:
Uppercase Alphabets: 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 Lowercase Alphabets: 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
Method 2: Using Character Variable and for loop
Implementation:
FileName: PrintAlphabets2.java
import java.util.*; public class PrintAlphabets2 { public static void main(String[] args) { // Printing uppercase alphabets using character variables System.out.println("Uppercase Alphabets:"); char upperCase = 'A'; // Starting with 'A' for uppercase for (; upperCase <= 'Z'; upperCase++) { // Looping through uppercase letters from 'A' to 'Z' System.out.print(upperCase + " "); // Printing each uppercase letter followed by a space } // Printing a newline for separation System.out.println(" "); // Printing lowercase alphabets using character variables System.out.println("Lowercase Alphabets:"); char lowerCase = 'a'; // Starting with 'a' for lowercase for (; lowerCase <= 'z'; lowerCase++) { // Looping through lowercase letters from 'a' to 'z' System.out.print(lowerCase + " "); // Printing each lowercase letter followed by a space } } }
Output:
Uppercase Alphabets: 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 Lowercase Alphabets: 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
- Using ASCII Values
- Using Character Variable
- Using a for-each loop
- Using for loop
- Using while loop
- Using do-while loop