loops

Java program to print all alphabets in given range using loops

Java program to print all alphabets in given range using loops

In this article, we will discuss the concept of Java program to print all alphabets in given range using loops

In this post, we are going to learn how to write a program to  print all alphabets in given range using  for, while and do-while  loop in Java language

Code to print all alphabets in given range

Code to print all alphabets in given range using for loop

In this code, we are going to learn how to print all alphabets in given range using for loop in Java language

Program 1

import java.util.Scanner;
public class Display_Alphabet_Letters{
public static void main(String args[]){
char ch,ch1,ch2;// declare character variable
 Scanner scan=new Scanner(System.in); 
  //create a scanner object for input
  
System.out.print("Enter the Starting Alphabets: ");
//ask starting alphabet from user
ch1=scan.next().charAt(0);
//Reading starting alphabets from user
System.out.print("Enter the Ending Alphabets ");
//ask ending alphabet from user
ch2=scan.next().charAt(0);
//Reading ending alphabets from user

System.out.print("Display Alphabets between"+ch1+" and "+ch2+" : \n");
for(ch=ch1; ch<=ch2; ch++){
System.out.print(ch+" ");
//display uppercase Alphabets with space using for loop
}
}

}

When the above code is executed, it producing the following result

Enter the Starting Alphabets: C
Enter the Ending Alphabets: M
Display Alphabets between C and M :
C  D  E  F  G  H  I  J  K  L  M

 

Code to print all alphabets in given range using while loop

In this code, we are going to learn how to print all alphabets in given range using while loop in Java language

Program 2

import java.util.Scanner;
public class Disp_Alphabet_Let1{
public static void main(String args[]){
char ch,ch1,ch2;// declare character variable
 Scanner scan=new Scanner(System.in); 
  //create a scanner object for input
  
System.out.print("Enter the Starting Alphabets ");
//ask starting alphabet from user
ch1=scan.next().charAt(0);
//Reading starting alphabets from user
System.out.print("Enter the Ending Alphabets ");
//ask ending alphabet from user
ch2=scan.next().charAt(0);
//Reading ending alphabets from user

System.out.print("Display Alphabets between "+ch1+" and "+ch2+" : \n");
ch=ch1; 
while(ch<=ch2){
System.out.print(ch+" ");
//display uppercase Alphabets with space using while loop
ch++;
}
}

}

When the above code is executed, it producing the following result

Enter the Starting Alphabets: S
Enter the Ending Alphabets: Z
Display Alphabets between S and Z :
S   T   U    V    W     X    Y    Z

 

Code to print all alphabets in given range using do-while loop

In this code, we are going to learn how to print all alphabets in given range using do-while loop in Java language

Program 3

import java.util.Scanner;
public class Disp_Alphabet_Let2{
public static void main(String args[]){
char ch,ch1,ch2;// declare character variable
 Scanner scan=new Scanner(System.in); 
  //create a scanner object for input
  
System.out.print("Enter the Starting Alphabets ");
//ask starting alphabet from user
ch1=scan.next().charAt(0);
//Reading starting alphabets from user
System.out.print("Enter the Ending Alphabets ");
//ask ending alphabet from user
ch2=scan.next().charAt(0);
//Reading ending alphabets from user

System.out.print("Display Alphabets between "+ch1+" and "+ch2+" : \n");
ch=ch1; 
do{
System.out.print(ch+" ");
//display uppercase Alphabets with space using do-while loop
ch++;
}while(ch<=ch2);
}

}

When the above code is executed, it producing the following result

Enter the Starting Alphabets: D
Enter the Ending Alphabets: M
Display Alphabets between D and M :
D   E   F    G    H    I    J    K   L    M


Suggested post

For loop in Java language

While loop in Java language

Do-while loop in Java language

Operator in Java language

Variable in Java language

Data type in Java language

class and main method in Java

 

Similar post

Java program to print all alphabets in given range

C program to print all alphabets in given range

C++ program to print all alphabets in given range

 

Java program to print all alphabets using loops

C program to print all alphabets using loops

C++ program to print all alphabets using loops

 

 

 

Karmehavannan

Recent Posts

Multiply two numbers in Java using scanner| 5 different ways

Multiply two numbers in Java using scanner| 5 different ways In this article, we will…

3 months ago

5 different ways to Divide two numbers in Java using scanner

5 Different ways to Divide two numbers in Java using scanner In this article, we…

3 months ago

Learn 8 Ways to Subtract Two Numbers Using Methods in Java

Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…

4 months ago

10 ways to subtract two numbers in Java

10 ways to subtract two numbers in Java In this article, we will discuss the…

4 months ago

Java Code Examples – Multiply Two Numbers in 5 Easy Ways

Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…

4 months ago

How to Divide two numbers in Java| 5 different ways

How to Divide two numbers in Java| 5 different ways In this article, we will…

4 months ago

This website uses cookies.