Exercise:
Write a Java Program to display uppercase and lowercase characters from A to Z using loop.
1.Display uppercase letters
Click Here to View the Solution!
public class DisplayCharacters {
public static void main(String[] args) {
char alphabet;
for(alphabet = 'A'; alphabet <= 'Z'; ++alphabet)
System.out.print(alphabet + " ");
}
}
Click Here to View the Output!
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
Click Here to View the Explanation!
- This program is used for displaying all the characters (uppercase letters) from A to Z by using a
for loop
. - Initially, only a single character variable
alphabet
is initialized. - A
for loop
is set by giving it a condition of iterating from the ASCII value of A till the ASCII value of Z. This looping through characters is possible because the characters are stored in the form of ASCII values in Java. - The increment in the character variable continues along with the printing of the characters until Z is reached. The loop initiates from the value 65 since ASCII value of A is 65 and up to 90 which is the ASCII value of Z.
2.Display Lowercase letters
Click Here to View the Solution!
public class DisplayCharacters {
public static void main(String[] args) {
char alphabet;
for(alphabet = 'a'; alphabet <= 'z'; ++alphabet)
System.out.print(alphabet + " ");
}
}
Click Here to View the Output!
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
Click Here to View the Explanation!
- This program is used for displaying all the characters (lowercase letters) from a to z by using a for loop.
- Initially, only a single character variable
alphabet
is initialized. - A for loop is set by giving it a condition of iterating from the ASCII value of a till the ASCII value of z. This looping through characters is possible because the characters are stored in the form of ASCII values in Java.
- The increment in the character variable continues along with the printing of the characters until z is reached. The loop initiates from the value 65 since ASCII value of a is 97 and up to 122 which is the ASCII value of z.