C program to check even or odd number using conditional operator

Write a C program to input a 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.

Example

Input

Input num: 10

Output

10 is even.

Required knowledge

Basic C programming, Arithmetic operators, Conditional operator

Learn this program using other approaches.

Learn 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 
     *     print it is even
     * else
     *     print it is odd
     */
    (num%2 == 0) 
        ? printf("The number is EVEN") 
        : printf("The number is ODD");

    return 0;
}

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 number from user */
    printf("Enter any number to check even or odd: ");
    scanf("%d", &num);

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

    return 0;
}

Output

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

Happy coding 😉

About Pankaj

Pankaj Prakash is the founder, editor and blogger at Codeforwin. He loves to learn new techs and write programming articles especially for beginners. He works at Vasudhaika Software Sols as a Software Design Engineer and manages Codeforwin. In short Pankaj is Web developer, Blogger, Learner, Tech and Music lover.

Follow on: Facebook | Twitter | Google | or

Comments and discussion
Have a doubt, write here. I will help my best.
Before commenting you must escape your source code before commenting. Paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>