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

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

3 months ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

3 months ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.