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

#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

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