Program to check whether a character is an alphabet, digit or special character.

Here, we have a basic program example to check if the character is a digit, alphabet or special character using different languages. This program is created in c language, c++, Java, and Python.

Program to check if the character is alphabet, digit or special character in C language

#include <stdio.h>
int main()
{
    char ch;
    printf("Input a character: ");
    scanf("%c", &ch);

    if((ch>='a' && ch<='z') || (ch>='A' && ch<='Z'))
    {
        printf("This is an alphabet.\n");
    }
    else if(ch>='0' && ch<='9')
    {
        printf("This is a digit.\n");
    }
    else
    {
        printf("This is a special character.\n");
    }
}

Program to check if the character is alphabet, digit or special character in C++ language

#include<iostream>
using namespace std;
int main()
{
    char ch;
    cout<<"Input a character: ";
    cin>>ch;

    if((ch>='a' && ch<='z') || (ch>='A' && ch<='Z'))
    {
        cout<<"This is an alphabet.\n";
    }
    else if(ch>='0' && ch<='9')
    {
        cout<<"This is a digit.\n";
    }
    else
    {
        cout<<"This is a special character.\n";
    }
}

Program to check if the character is alphabet, digit or special character in Python language

ch=input("Enter a character:  ")
if((ch>='a' and ch<='z') or (ch>='A' and ch<='Z')):
    print("This is an alphabet.")
elif(ch>='0' and ch<='9'):
    print("This is a digit.")
else:
    print("This is a special character.")

Program to check if the character is alphabet, digit or special character in Java language

import java.util.*;
class character
 {
   public static void main(String[] args)
    {
       
      char ch;

      Scanner s=new Scanner(System.in);
      System.out.println("Input a character: ");
      ch = s.next().charAt(0);

      if((ch>='a' && ch<='z') || (ch>='A' && ch<='Z'))
    {
       System.out.println("This is an alphabet.");
    }
    else if(ch>='0' && ch<='9')
    {
       System.out.println("This is a digit.");
    }
    else
    {
        System.out.println("This is a special character.");
    }
}
}