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

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.

Required Knowledge


C program to check for decimal digit characters using conditional operator

#include <stdio.h>  
  
int main() {  
    char c;
    int isDigit;  
  
    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 Digit\n", c);
    else 
        printf("%c is Not a Digit\n", c);
  
    return 0;  
} 
Output
Enter a Character
7
7 is Digit
Enter a Character
A
A is Not a Digit

Related Topics
C program to check whether a character is vowel or consonant using switch statement
C program to check whether a character is alphabet or not
C program to check whether a character is alphabet or digit
C program to check whether a number is positive, negative or zero
C program to check year is leap year or not
C program to swap two numbers
C program to check whether a number is perfect number
C program to check whether a number is prime or not
C program to convert string to integer
List of all C programs