Java program to check a character is an alphabet or not

A character is an alphabet or not using java

Here, in this section we will discuss the program to check whether the character is an Alphabet or not using java. A character will be an alphabet,  if and only if it is either in uppercase letter or in lowercase letter. In this article we will discuss whether a particular character entered by the user is an alphabet or not using java programming language.

character is an Alphabet or not using java

Explanation

In C programming language a char type variable can store many different types of characters-

  • Alphabets (a, b, c… )
  • Digits(1, 2, 3…)
  • Special characters(@, %, &…)

These characters are differentiated on the basis of ASCII values :

  • between 65 and 90 for upper case(A, B, C…)
  • between 97 and 122 for lower case(a, b, c…)

In here we will see how to identify whether a character is alphabet or not using C++ programming language.

Working

  • Get user input
  • Check if input is between ‘A'(65) – ‘Z'(90) or between ‘a'(96) – ‘z'(122)
  • If True print ‘Yes’
  • If False print ‘No’
Character is an alphabet or not

Java code (method 1)

Run

 

//Java program to check whether the character entered by the user is an alphabet or not.
import java.util.Scanner;

//class declaration
public class Main
{						
    
    //main method declaration
    public static void main(String[] args)
    {										
        char ch;
                              
        ch = '9';
		
	//condition for checking characters
        if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
	      System.out.println("The inserted character " + ch + " is an Alphabet");
	else
	      System.out.println("The inserted character " + ch + " is not an Alphabet");
		
				                               
	
    } //end of the main method
    
} //end of the class

Output

Insert any character: 9
The entered character 9 is not an Alphabet

You can also check the alphabet using the ASCII values of characters like this:

if((ch >= 97 && ch <= 122) || (ch >= 65 && ch <= 90))
      System.out.println("The inserted character " + ch + " is an Alphabet");
else
      System.out.println("The inserted character " + ch + " is not an Alphabet");

Java code (method 2)

Run
//Java program to check whether the character entered by the user is an alphabet or not.
import java.util.Scanner;

//class declaration
public class Main
{						
    
    //main method declaration
    public static void main(String[] args)
    {										
        char ch;
                         
        ch = 'k';
		
	//condition for checking characters
        if((ch >= 97 && ch <= 122) || (ch >= 65 && ch <= 90))
	      System.out.println("The inserted character " + ch + " is an Alphabet");
	else
	      System.out.println("The inserted character " + ch + " is not an Alphabet");
		
				                               
	
    } //end of the main method
    
} //end of the class

Output

The entered character k is an Alphabet

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

One comment on “Java program to check a character is an alphabet or not”


  • Shubham

    public class JavaApplication1
    {
    public static void main(String[] args)
    {
    Scanner obj = new Scanner(System.in);
    char c = obj.next().charAt(0);
    if(c>=’a’&&c<='z')
    System.out.print("Yes");
    else
    System.out.println("No");
    }

    }