Program to Display all Alphabet between given range using loops in Java
Program to Display all Alphabet between given range using loops in Java
In this article, we will discuss the concept of Program to display all Alphabet between given range using loops in Java programming language
In this post, we are going to learn how to display all the upper case(A to Z) and lower case (a to z) Alphabets between given range using the loops in Java language
Program to display all Alphabet using for loop
The program displays all the upper case and lower case alphabet letters between given range using for loop in Java language
Program 1
import java.util.Scanner; public class Display_Alphabet_1{ public static void main(String args[]){ char ch,ch1,ch2;//char variable declaration Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.print("Enter the Starting Alphabets: "); ch1=scan.next().charAt(0);//store entered first Alphabet in variale ch1 System.out.print("Enter the Ending Alphabets: "); ch2=scan.next().charAt(0);//store entered last Alphabet in variale ch2 for(ch=ch1; ch<=ch2; ch++){ System.out.print(ch+" "); //display Alphabets with space } } }
When the above code is executed, it produces the following result
Case 1
Enter the starting Alphabets: d Enter the Ending Alphabets: v d e f g h i j k l m n o p q r s t u v
Case 2
Enter the starting Alphabets: F Enter the Ending Alphabets: X F G H I J K L M N O P Q R S T U V W X
In the above program, we are using a for loop, this is used to print upper case Alphabet letters or lower case Alphabet letters in the given range
Approach
- In the above program, the user is asked to enter two Alphabet and entered Alphabet is stored in character variable ch1,ch2;
- In this for loop, it is initialized by i=ch1 and checks the condition of (i<=ch2);
- If the test expression is true, the program is printed the Alphabets letters between given Alphabets
- If the test expression is false, the loop is terminated
Program to print all Alphabet using while loop
Program 2
The program displays all the upper case and lower case alphabet letters between given range using while loop in Java language
import java.util.Scanner; public class Display_Alphabet_2{ public static void main(String args[]){ char ch,ch1,ch2;//char variable declaration Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.print("Enter the Starting Alphabets "); ch1=scan.next().charAt(0);//store entered first Alphabet in variale ch1 System.out.print("Enter the Ending Alphabets "); ch2=scan.next().charAt(0);//store entered last Alphabet in variale ch2 ch=ch1; while(ch<=ch2){ System.out.print(ch+" "); //display Alphabets with space ch++; } } }
When the above code is executed, it produces the following result
Case 1
Enter the starting Alphabets: c Enter the Ending Alphabets: l c d e f g h i j k l
Case 2
Enter the starting Alphabets: H Enter the Ending Alphabets: U H I J K L M N O P Q R S T U
In the above program, we are using a while loop, this is used to print upper case Alphabet letters or lower case Alphabet letters
Approach
- In the above program, the user is asked to enter two Alphabet and entered Alphabet is stored in character variable ch1,ch2;
- In this while loop, it is initialized by i=ch1 and checks the condition of (i<=ch2);
- If the test expression is true, the program is printed the Alphabets letters between given Alphabets
- If the test expression is false, the loop is terminated
Program to print all Alphabet using do-while loop
Program 3
The program displays all the upper case and lower case alphabet letters between the given range using the do-while loop in Java language
import java.util.Scanner; public class Display_Alphabet_3{ public static void main(String args[]){ char ch,ch1,ch2;//char variable declaration Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.print("Enter the Starting Alphabets "); ch1=scan.next().charAt(0);//store entered first Alphabet in variale ch1 System.out.print("Enter the Ending Alphabets "); ch2=scan.next().charAt(0);//store entered last Alphabet in variale ch2 ch=ch1; do{ System.out.print(ch+" "); //display Alphabets with space ch++; }while(ch<=ch2); } }
When the above code is executed, it produces the following result
Case 1
Enter the starting Alphabets: d Enter the Ending Alphabets: u d i j k l m n o p q r s t u
Case 2
Enter the starting Alphabets: J Enter the Ending Alphabets: Y J K L M N O P Q R S T U V W XY
In the above program, we are using a do-while loop, this is used to print upper case Alphabet letters or lower case Alphabet letters
Approach
- In the above program, the user is asked to enter two Alphabet and entered Alphabet is stored in character variable ch1,ch2;
- In this do-while loop, it is initialized by i=ch1 and checks the condition of (i<=ch2);
- If the test expression is true, the program is printed the Alphabets letters between given Alphabets
- If the test expression is false, the loop is terminated
Suggested for you
do-while loop in Java language
Similar post
Java program to print all upper case and lower case Alphabets
C++ program to print all upper case and lower case Alphabets
C program to print all upper case and lower case Alphabets
Java code to print all upper case and lower case Alphabets between the given range
C++ code to print all upper case and lower case Alphabets between the given range
C code to print all upper case and lower case Alphabets between the given range