C Program to Check Whether a Number is Even or Odd

Howdy readers, today you will learn how to write a program to check whether a number is even or odd using C Programming language.

C Program to Check Whether a Number is Even or Odd

If any number is exactly divisible by 2, then it is an even number otherwise it’s an odd number.

You can also say any number modulo(%) divided by 2 equals to 0 is an even number. We will be using the Modulus(%) operator in our program to check whether a number is even or odd.

We will write this program using the following methods:

  1. Using Modulus(%) operator
  2. Using Bitwise operator
  3. Using User-defined Function

So, without any delay, let’s begin this tutorial.

C Program to Check Whether a Number is Even or Odd

C Program

// C Program to Check Whether a Number is Even or Odd
#include <stdio.h>
int main(){
    int num;
    
    // Asking for input
    printf("Please enter a number: ");
    scanf("%d", &num);
    
    // Check even or odd
    if (num % 2 == 0){
        printf("%d is an even number.", num);
    }
    else {
        printf("%d is an odd number.", num);
    }
    
    return 0;
}

Output 1

Please enter a number: 7
7 is an odd number.

Output 2

Please enter a number: 68
68 is an even number.
C Program to Check Whether a Number is Even or Odd

Explanation

    int num;

In this program, we have declared an int data type variable named num.

    // Asking for input
    printf("Please enter a number: ");
    scanf("%d", &num);

Then, the user is asked to enter a number. This number gets stored in the num named variable.

    if (num % 2 == 0){
        printf("%d is an even number.", num);
    }
    else {
        printf("%d is an odd number.", num);
    }

Now, we check whether the entered number is divisible by 2 or not.

If the entered number is divisible by 2, then it is an even number otherwise it’s an odd number.

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

C Program

// C Program to Check Whether a Number is Even or Odd Using Bitwise Operator
#include <stdio.h>
int main(){
    int num;
    
    // Asking for input
    printf("Enter an integer: ");
    scanf("%d", &num);
    
    // Even or Odd
    if (num & 1){
        printf("%d is an odd number.", num);
    }
    else {
        printf("%d is an even number.", num);
    }
    
    return 0;
}

Output

Enter an integer: 15
15 is an odd number.
C Program to Check Whether a Number is Even or Odd Using Bitwise Operator

C Program to Check Whether a Number is Even or Odd Using Functions

C Program

// C Program to Check Whether a Number is Even or Odd Using Functions
#include <stdio.h>
int even_odd(int n){
    if (n % 2 == 0)
        printf("%d is an even number.", n);
    else
        printf("%d is an odd number.", n);
}

int main(){
    int num;
    
    // Asking for input
    printf("Enter a number: ");
    scanf("%d", &num);
    
    // Calling custom function
    even_odd(num);
    
    return 0;
}

Output

Enter a number: 47
47 is an odd number.
C Program to Check Whether a Number is Even or Odd Using Functions

Explanation

int even_odd(int n){
    if (n % 2 == 0)
        printf("%d is an even number.", n);
    else
        printf("%d is an odd number.", n);
}

In this program, we have defined a custom function named even_odd which checks whether a number is odd or even.

    // Calling custom function
    even_odd(num);

Then, we called this custom function in the main function to display the result.

Conclusion

I hope after reading this post, you understand how to write a program to check whether a number is even or odd using C Programming language.

If you have any doubt regarding this program, then let us know in the comment section. We will be delighted to solve your query.

Also read:

Leave a Comment