C Program to Check a Number is Odd or Even Using Conditional Operator

  • Write a C program to check whether a number is odd or even using conditional operator.
  • Wap in C to check whether a number is odd or even using ternary operator

Required Knowledge

  • C printf and scanf functions
  • Conditional Operator in C

In this program, we will use conditional operator to check a number is odd or even.

C program to check whether a number is odd or even using conditional operator

C program to check whether a number is odd or even using conditional operator

#include <stdio.h>  
   
int main() {  
    int num, isEven;  
   
    /* Take a number as input from user
  using scanf function */
    printf("Enter an Integer\n");  
    scanf("%d", &num); 
     
    /* Finds input number is Odd or Even 
 using Ternary Operator */ 
    isEven =  (num%2 == 1) ? 0 : 1;  
     
    if(isEven == 1)
        printf("%d is Even\n", num);
    else
        printf("%d is Odd\n", num);
   
    return 0;  
}

Output

Enter an Integer
3
3 is Odd
Enter an Integer
4
4 is Even

Leave a Comment