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

@codevarsity
 · 
1.3k views
 · 

In this example, conditional statement is used to check whether a number entered by the user is even or odd.

Code

#include <stdio.h>
int main()
{
    int number;

    printf("Enter an integer: ");
    scanf("%d", &number);

    (number % 2 == 0) ? printf("%d is even.", number) : printf("%d is odd.", number);

    return 0;
}

Output

Enter an integer: -7
-7 is odd.

Comments