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 ;)


You may also like

Labels: , ,