Write a program in C++ and python to check whether a given character is an alphabet, digit or any special character

By | August 1, 2019

Here in this program, we will determine whether the type of the entered input.

C++:

#include<iostream.h>
#include< conio.h>
#include<stdio.h>
#include<math.h>

void main()
{

clrscr();
char ch;
cout<<"Enter a character: " ;
cin>>ch;
if((ch>='A '&& ch<='Z')||(ch>='a' && ch<='z'))
cout<<"It is an alphabet";
else
{
if (ch>='0'&&ch<='9')
cout<<"It is a digit";
else
cout<<"It is a special character";
}
getch();
}

Output:

Enter a character: 4
It is a digit

Python:

y= input("Enter a character: ")
k=0
if (y>='a' and y<='z') or (y>='A' and y<='Z'):
    print("It is an alphabet")
    k=1

if y.isdigit():
    print("It is a digit")
    k=1

if k==0:
   print("It is a special character ")

Output:

Enter a character: $
It is a special character

Leave a Reply

Your email address will not be published. Required fields are marked *