C program to check alphabets using Conditional/Ternary operator

Write a C program to enter any character and check whether character is alphabet or not using Conditional/Ternary operator (?:).

Also view this program using if else -
C program to check whether a character is alphabet or not using if else.

Required knowledge:

Basic C programming, Conditional operator.

Program:

/**
 * C program to check alphabets using Conditional operator
 */

#include <stdio.h>

int main()
{
    char ch;
    
    /*
     * Reads a character from user
     */
    printf("Enter any character: ");
    scanf("%c", &ch);
    
    printf("It is %s", (((ch>='a' && ch<='z') || (ch>='A' && ch<='Z')) ? "ALPHABET" : "NOT APLHABET") );

    return 0;
} 
Output
X
_
Enter any character: a

It is ALPHABET

Happy coding ;)


Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< code >< pre > -- Your source code -- < /pre >< /code > (Remove spaces from pre and code tags).

No comments:

Post a Comment