Home / Programs / Write a short program to find whether the given character is a digit or a letter.
Programming Example

Write a short program to find whether the given character is a digit or a letter.

👁 11 Views
💻 Practical Program
📘 Step by Step Learning
Study this program carefully to understand the logic, output, and explanation in a structured way.

Information & Algorithm

Given Input:

Enter a character: A

Expected Output:

It is a Letter.

Given Input:

Enter a character: 7


Expected Output:

It is a Digit.

Program Code

import java.util.Scanner;

public class CheckCharacter {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a character: ");
        char ch = sc.next().charAt(0);

        if (Character.isDigit(ch)) {
            System.out.println("It is a Digit.");
        } 
        else if (Character.isLetter(ch)) {
            System.out.println("It is a Letter.");
        } 
        else {
            System.out.println("It is neither a Digit nor a Letter.");
        }

        sc.close();
    }
}

How to learn from this program

First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.