C Program To Check Whether a Number is Even or Odd

In this post, we will write a program to check whether a Number is Even or Odd.

A number which is exactly divisible by 2 is called an Even Number. For Example – 2, 32, 256 etc.

A number which is not exactly divisible by 2 is called an Odd Number. For Example – 3, 17, 247 etc.

C Program To Check Whether a Number is Even or Odd

We will be using different methods to check whether the number entered by the user is Even or Odd.

So, Without Further Ado, let’s begin the tutorial.

C Program To Check Whether a Number is Even or Odd

// C Program To Check Whether a Number is Even or Odd
#include <stdio.h>
int main(){
    int num;
    
    // Asking for Input
    printf("Enter the Number: ");
    scanf("%d", &num);
    
    // Even if num is divisible by 2
    if (num % 2 == 0)
        printf("%d is an Even Number", num);
    else
        printf("%d is an Odd Number", num);
    
    return 0;
}

Output

Enter the Number: 13
13 is an Odd Number

How Does This Program Work ?

 int num;

Here, in this program we have declared an int variable num.

    // Asking for Input
    printf("Enter the Number: ");
    scanf("%d", &num);

Then, the user is asked to input the input. The integer entered by the user is stored in the num variable.

    // Even if num is divisible by 2
    if (num % 2 == 0)
        printf("%d is an Even Number", num);
    else
        printf("%d is an Odd Number", num);

If the num is perfectly divisible by 2 ,the expression evaluates to True. This means the number is Even.

And If the num is not perfectly divisible by 2, then in this case the expression evaluates to 0 (False). This states the number is Odd.

Some used terms are as follow:

#include <stdio.h> – In the first line we have used #include, it is a preprocessor command that tells the compiler to include the contents of the stdio.h(standard input and output) file in the program.

The stdio.h is a file which contains input and output functions like scanf() and printf() to take input and display output respectively. 

Int main() – Here main() is the function name and int is the return type of this function. The Execution of any Programming written in C language begins with main() function. 

scanf() – scanf() function is used to take input from the user. 

printf() – printf() function is used to display and print the string under the quotation to the screen.

% – % (Modulus Operator) is used for division.

If. . . else – An if statement can be followed by an optional else statement, which executes when the Boolean expression is false. If Statement executes when the Boolean expression is True.

// – Used for Commenting in C

Using Bitwise Operator

// C Program To Check Whether a Number is Even or Odd
#include <stdio.h>
int main(){
    int num;
    
    // Asking for Input
    printf("Enter the Number: ");
    scanf("%d", &num);
    
    if (num & 1)
        printf("%d is an Odd Number", num);
    else 
        printf("%d is an Even Number", num);
        
    return 0;    
}

Output

Enter the Number: 86
86 is an Even Number
    if (num & 1)
        printf("%d is an Odd Number", num);
    else 
        printf("%d is an Even Number", num);

num & 1 is a Bitwise AND operation of the num variable and 1 literal.

Using Ternary Operator

// C Program To Check Whether a Number is Even or Odd
#include <stdio.h>
int main(){
    int num;
    
    // Asking for Input
    printf("Enter the Number: ");
    scanf("%d", &num);
    
    (num % 2 == 0) ? printf("%d is an Even Number", num) : printf("%d is an Odd Number", num);
    
    return 0;
}

Output

Enter the Number: 18
18 is an Even Number
(num % 2 == 0) ? printf("%d is an Even Number", num) : printf("%d is an Odd Number", num);

In this program, we have used ? : (ternary operator) instead of the if. . . else statement.

Conclusion

I hope after reading this post, you understand how to check whether a number is Even or Odd in C Programming language. 

If you have any doubt regarding the topic, feel free to contact us in the Comment Section. We will be delighted to help you.

Leave a Comment

Your email address will not be published. Required fields are marked *