C Program to Check Whether a Character is Decimal Digit or Not using Conditional Operator

  • Write a C program to check a character is decimal digit or not using conditional or ternary operator.

Required Knowledge

In this program, we will check whether the ASCII value of input character(C)is in between the ASCII value of ‘0’ and ‘9’ decimal digit character(including ‘0’ and ‘9’).

In other words, If ‘0’ <= C <= ‘9’ is true, then C is a decimal digit character.

C program to check for decimal digit characters using conditional operator

C program to check for decimal digit characters using conditional operator

#include <stdio.h>  
   
int main() {  
    char c;
    int isDigit;  
   
    /* Take a character as input from user
  using scanf function */
    printf("Enter a Character\n");  
    scanf("%c", &c); 
     
    /* Check, If input character is digit */ 
    isDigit =  ((c >= '0') && (c <= '9'))? 1 : 0;  
     
    if(isDigit == 1)
        printf("%c is Decimal Digit Character\n", c);
    else
        printf("%c is Not a Digit Character\n", c);
   
    return 0;  
}

Output

Enter a Character
7
7 is Decimal Digit Character
Enter a Character
A
A is Not a Digit Character