Any character is entered through the keyboard, write a program to determine whether the character entered is a capital letter, a small case letter, a digit or a special symbol. The following table shows the range of ASCII values for various characters.

ASCII value character classification

Below is a Java program that reads a character input from the keyboard and determines whether it is a capital letter, a small case letter, a digit, or a special symbol based on its ASCII value.

				
					import java.util.Scanner;

public class CharacterType {
    public static void main(String[] args) {
        // Create a Scanner object for input
        Scanner scanner = new Scanner(System.in);
        
        // Prompt the user to enter a character
        System.out.print("Enter a character: ");
        char ch = scanner.next().charAt(0);
        
        // Get ASCII value of the character
        int asciiValue = (int) ch;
        
        // Determine the type of character using ASCII values
        if (asciiValue >= 65 && asciiValue <= 90) {
            System.out.println(ch + " is a capital letter.");
        } else if (asciiValue >= 97 && asciiValue <= 122) {
            System.out.println(ch + " is a small case letter.");
        } else if (asciiValue >= 48 && asciiValue <= 57) {
            System.out.println(ch + " is a digit.");
        } else if ((asciiValue >= 0 && asciiValue <= 47) || 
                   (asciiValue >= 58 && asciiValue <= 64) ||
                   (asciiValue >= 91 && asciiValue <= 96) ||
                   (asciiValue >= 123 && asciiValue <= 127)) {
            System.out.println(ch + " is a special symbol.");
        } else {
            System.out.println("Invalid input.");
        }
        
        // Close the scanner
        scanner.close();
    }
}

				
			
prime number

Explanation:

  1. Scanner Class: Used to read input from the user.
  2. charAt(0): Reads the first character of the input string.
  3. if-else Statements: Determines the type of character based on its ASCII value:
    • Capital letters: ASCII values between 65 (‘A’) and 90 (‘Z’).
    • Small case letters: ASCII values between 97 (‘a’) and 122 (‘z’).
    • Digits: ASCII values between 48 (‘0’) and 57 (‘9’).
    • Special symbols: Any other characters.

Sample Output:

				
					Enter a character: A
A is a capital letter.

Enter a character: g
g is a small case letter.

Enter a character: 5
5 is a digit.

Enter a character: @
@ is a special symbol.

				
			

This program correctly identifies the type of character based on the ASCII value ranges provided.

Scroll to Top