Java Example To Print All Alphabets

Sharing is healthy !!

Today you will learn java example to print all alphabets.

Here in the below example we are going to show you three methods to print alphabets in java.

Java Example To Print all Alphabets

Java Example To Print All Alphabets

method one: using for loop

Also Read – Java Program To Print Floyd Triangle

public class Print_Alphabets_In_Java {

public static void main(String[] args) {
 
System.out.println("Printing Alphabets from a to z: "); 
char alpha;
 
for(alpha = 'a';alpha <= 'z';alpha++)
System.out.println(alpha);
}
}

Output:

Printing Alphabets from a to 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


method two: using while loop

public class Print_Alphabets_In_Java {

public static void main(String[] args) {
 
System.out.println("Printing Alphabets from a to z: "); 
char alpha = 'a';
 
while(alpha <= 'z'){
 
System.out.println(alpha);
alpha++;
 
}
}
}

Output:

Printing Alphabets from a to 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


method three: using do while loop
public class Print_Alphabets_In_Java {

public static void main(String[] args) {
 
System.out.println("Printing Alphabets from a to z: "); 
char alpha = 'a';
 
do{
 
System.out.println(alpha);
alpha++;
 
}while(alpha <= 'z');
}
}

Output:

Printing Alphabets from a to 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


However, above java example to print all alphabets can also be used to print few alphabets, that is, from alphabet ‘a’ to alphabet ‘m’. See an example below,

public class Print_Alphabets_In_Java {

public static void main(String[] args) {
 
System.out.println("Printing Alphabets from a to m: ");
char alpha = 'a';
 
do{
 
System.out.println(alpha);
alpha++;
 
}while(alpha <= 'm');
}
}

Output:

Printing Alphabets from a to m: a b c d e f g h i j k l m


Also, in the above java example to print all alphabets, alphabets can be printed in uppercase letters(above example output is in lowercase). Let’s see an example,

public class Print_Alphabets_In_Java {

public static void main(String[] args) {
 
System.out.println("Printing Alphabets from A to Z: "); 
char alpha;
 
for(alpha = 'A';alpha <= 'Z';alpha++)
System.out.println(alpha);
 
}
}

Output:

Printing Alphabets from A to 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

Sharing is healthy !!

Leave a Reply

Your email address will not be published.