Algorithm


  1. Input:

    • Accept a character as input.
  2. Check if the character is an alphabet:

    • Use the Character.isLetter(char ch) method from the Character class in Java to check if the given character is an alphabet.
      • If Character.isLetter(ch) returns true, then the character is an alphabet.
      • If Character.isLetter(ch) returns false, then the character is not an alphabet.
  3. Output:

    • Display the result based on the check performed in step 2.

 

Code Examples

#1 Code Example- Java Program to Check Alphabet using if else

Code - Java Programming

public class Alphabet {

    public static void main(String[] args) {

        char c = '*';

        if( (c >= 'a' && c <= 'z'> || (c >= 'A' && c  < = 'Z'))
            System.out.println(c + " is an alphabet.");
        else
            System.out.println(c + " is not an alphabet.");
    }
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
* is not an alphabet.

#2 Code Example- Java Program to Check Alphabet using ternary operator

Code - Java Programming

public class Alphabet {

    public static void main(String[] args) {

        char c = 'A';
        
        String output = (c >= 'a' && c  < = 'z') || (c >= 'A' && c <= 'Z')
                ? c + " is an alphabet."
                : c + " is not an alphabet.";
        
        System.out.println(output);
    }
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
A is an alphabet.

#3 Code Example - Java Program to Check Alphabet using isAlphabetic() Method

Code - Java Programming

class Main {
  public static void main(String[] args) {

    // declare a variable
    char c = 'a';

    // checks if c is an alphabet
    if (Character.isAlphabetic(c)) {
      System.out.println(c + " is an alphabet.");
    }
    else {
      System.out.println(c + " is not an alphabet.");
    }
  }
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
a is an alphabet.
Advertisements

Demonstration


Java Programing Example to Check Whether a Character is Alphabet or Not-DevsEnv