In the previous article we have discussed about Java Program to Find ASCII Value of a Character
In this article we are going to see how to print/display all the alphabets using Java
Java Program to Print Alphabets from A to Z
We have 26 alphabets starting from “A” to “Z” or “a” to “z”. So we have to run a loop from “A” to “Z” so that we can get all the alphabets in output.
Method-1: Java Program to Print Alphabets from A to Z By Using For Loop
Approach:
- Taking a character variable as ch.
- Start a for loop and the value of ch should be initiated with “A” or “a” and goes up to “Z” or “z”.
- The value of ch is increasing by one in the for loop.
- Inside the for loop we are continuously print the value of ch so that we get the result.
Program:
class Main { public static void main(String args[]) { char ch; //using for loop start from ‘A’ and continue upto ‘Z’ for(ch= 'A';ch<='Z';++ch) System.out.print(ch+" "); } }
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
Method-2: Java Program to Print Alphabets from A to Z By Using While Loop
Approach:
- Define a method called
computeValue()
without any argument. - In method definition create a character variable named as
ch
and assigned a value “A” or “a” to it. - Now create a while loop and inside the while loop’s condition put ch<=’Z’ or ch<=’z’.
- Giving a print statement inside the while loop so that it will print all the value of ch throughout the loop.
- Now just call this method in main function.
- Print the result.
Program:
class Main { //define a method public static void computeValue() { //initiate the value of ch char ch= 'A'; //declaring while loop while(ch<='Z') { System.out.print(ch+" "); ch+=1; } } public static void main(String args[]) { computeValue(); } }
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
Guys who are serious about learning the concepts of the java programming language should practice this list of programs in java and get a good grip on it for better results in exams or interviews.
Related Java Programs: