2 approaches to print Alphabets in java
In this article you can learn how to print Alphabets i.e A to Z in java programming language with 2 different approaches.
- Here I will discuss for both upper case alphabets and lower case alphabet.
Approach :- 1 "print upper case alphabets using char"
public class upppperCaseBychar {
public static void main(String[] args) {
System.out.println("The upper case alphabets using char");
for(char i = 'A'; i <= 'Z'; i++){
System.out.print(i+" ");
}
}
Output
Approach :- 2 "print upper case alphabets using int"
public class upperCaseByint {
public static void main(String[] args){
System.out.println("The upper case alphabets using int");
for(int i = 65; i <= 90; i++){
//By type casting from int to char
System.out.print((char)i+" ");
}
}
Approach :- 3 "print lower case alphabets using char"
public class lowerCaseBychar {
public static void main(String []args){
System.out.println("lower case alphabets using char");
for(char i = 'a'; i <= 'z'; i++){
System.out.print(i+" ");
}
}
Output
Approach :- 4 "print lower case alphabets using int"
public class lowerCaseByint {
public static void main(String[] args){
System.out.println("lower case alphabets using int");
for(int i = 97; i <= 122; i++){
System.out.print((char)i+" ");
}
}
Code Explanation:-
Here I will explain how both char and int methods are working.
Approach :- 1 "char method"
- Here I have started the loop from A and it will go upto Z and will print all the alphabets. For lower case also, you have to apply same concept.
Approach :- 2 "int method"
- In this approach ASCII value plays a major role.
- The ASCII value of A is 65 and Z is 90, so we have to start the loop from 65 upto 90. It will print all the upper case alphabets.
- Similarly, the ASCII value of a is 97 and z is 122. So if we start the loop from 97 to 122, it will print all the lower case alphabets.
👉 If you learn something new from this article please share with your friends.
- if you have any questions or doubt, please write it on the comment below.