Jan 7, 2012

Program: To check given character is Alphabet, Digit or any Other

 Program: To check given character is Alphabet, Digit or any Other


#include<iostream.h>
#include<conio.h>
//check given character is alphabets, digit or any other character

int main()
{
    char a;
    cout <<"Enter a character : \n";
    cin >> a;
    if(((a >= 'A') && (a<='Z')) || ((a >='a') && (a <= 'z')))
    cout << "You entered an Alphabet\n";
    else if(a >= '0' && a <='9')
    cout << "You entered a digit\n";
    else
    cout << "You entered non alpha numeric character\n";
  
    /* i define char a that contain only one character and then i take input
    from user. and then i set condition to check what type of data user enter
    first condition that i set a >= 'A' && a<='Z' and in these i write two condition]
    joining with && now understand the function of && this is used to check that both the condition is
    true or false like here i set a>= 'A' && a<='Z' this condition run when both is true
    this condition mean that value is between A & Z and now see full condition
    that condition i for uppercase now check for lower case we separate condition
    with OR sign '||' this mean condition must true either one of the side must true
    like here (((a >= 'A') && (a<='Z')) || ((a >='a') && (a <= 'z')))
    first uppercase check if it false then it check lower case either one of these
    true then only it execute then it print you enter alphabet
    
    Now second condition you can see to check number (a >= '0' && a <='9')
    i use else if condition u can also use if condition under else
    like else
    {
        if((a >= '0' && a <='9')
        cout<<"You enter Number";
    }
    
    like this you can also give its same the above condition check the number
    between 0 to 9
    
    and third is else part other than above number or alphabet it execute
    that part
    */
    getch();
    return 0;
}



OUTPUT



1 comment: