C program to check even or odd number using conditional operator

Write a C program to enter any number and check whether number is even or odd using Conditional/Ternary operator ?:. How to check even or odd numbers using conditional operator in C program. Checking whether a given number is even or odd using ternary operator in C programming.

Example

Input

Input num: 10

Output

10 is even.

Required knowledge

Basic C programming, Conditional operator

Also view this program using other approaches.

Read more -

Program to check even or odd

/**
 * C program to check even or odd number using conditional operator
 */

#include <stdio.h>

int main()
{
    int num;

    /*
     * Input a number from user
     */
    printf("Enter any number to check even or odd: ");
    scanf("%d", &num);

    /* if(n%2==0) then it is even */
    (num%2 == 0) ? printf("The number is EVEN") : printf("The number is ODD");

    return 0;
}

Note: Another approach of writing the same program as.

**
 * C program to check even or odd number using conditional operator
 */

#include <stdio.h>

int main()
{
    int num;

    /*
     * Input a number from user
     */
    printf("Enter any number to check even or odd: ");
    scanf("%d", &num);

    /* Print Even if (n%2==0) */
    printf("The number is %s", (n%2==0 ? "EVEN" : "ODD"));

    return 0;
}

Output

Enter any number to check even or odd: 20
The number is EVEN

Happy coding 😉

Write your doubts or suggestion. I will try my best to help. You must escape source code before commenting. To format your source code paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>