In this article, we will write a C program to check whether a given number is even or odd. The program will take a number from the user as input, check if it is even or odd and print the output on the output screen.
Sample Examples:
Input: N = 10
Output: 10 is an Even number
Explanation: Because 10 is divisible by 2, therefore, it is an Even number.
Input: N = 11
Output: 11 is an Odd number
Explanation: Because 11 is not divisible by 2, therefore, it is an Odd number.
A number which is completely divisible by 2 is called an even number and a number which is not completely divisible by 2 is called an odd number.
For Example: If we divide the number 12 by 2, then its remainder will be 0 which means 12 is completely divisible by 2, therefore, 12 is an even number.
Similarly, if we divide the number 13 by 2 then the remainder will be 1 which means 13 is not completely divisible by 2, so 13 is an odd number.
In C programming, if we want to get the remainder of the division of two numbers, we can use the modulus operator %
. For eg: a % b
will give us the remainder of a
dividing by b
.
Following is the code in C Programming to check whether a number is even or odd using the modulo operator (%
).
// C program check whether a number is Even or Odd. #include<stdio.h> int main() { int number; // Ask the user for input printf("Enter a number: "); scanf("%d", &number); // Check if remainder is 1 or 0 if(number % 2 == 0){ printf("%d is an Even number", number); } else{ printf("%d is an Odd number", number); } return 0; }
Output1:
Enter a number: 10 10 is an Even number
Output2:
Enter a number: 5 5 is an Odd number
In this program, we have first declared a variable number
which is of type int
. The program then ask the user to enter a number with the help of printf()
and scanf()
functions.
The number Entered by the user is stored in the variable number
, after that in the next line, we check whether the number is even or odd by checking the remainder of number % 2
.
Finally, the output is printed on the output screen using the printf()
function based on the satisfied condition.
Method-2: Using Bitwise AND Operator (&)
We can also use the bitwise AND(&
) operator to check whether a number is prime or not.
In this method, we check the last bit of the given number. If the last bit of the number is 1 then the number will be odd, otherwise it will be an even number.
For example, the binary equivalent of 5 is (101)2 . Here, the last digit is 1 which means it is an Odd number. Similarly, the binary equivalent of 4 is (100)2 . Here, the last digit is 0, which means it is an Even number.
Bitwise operators are a type or operator in C programming which performs the operations on bit level (binary level) where each digit in a binary number represents a single bit.
Bitwise AND (&) is one of the bitwise operators which is used to perform the bitwise AND operation between individual bits of the binary representation of two numbers.
For each pair of bits in the number, if both the bits are 1 then the result will be 1 otherwise in all other cases the result will be 0.
Example: Performing bitwise AND operation between the numbers 13 and 1.
The binary value of 13 is 00001101
and the binary value of 1 is 00000001
.
Performing bitwise AND (&) operation between the numbers 13 and 1.
00001101
& 00000001
------------------------
00000001
------------------------
Performing bitwise AND (&) operation between the numbers 14 and 1.
00001110
& 00000001
------------------------
00000000
-------------------------
As you can see in the above explanation, after performing the bitwise &
operation between the numbers 13 and, 1 we are getting the result 1. So, the number 13 is an odd number.
In the same way, after performing the bitwise &
operation between 14 and 1 we are getting 0. So, the number 14 is an even number.
Following is the code in c programming to check whether a number is even or odd using the Bitwise AND operator (&
).
// C program to check whether a number is even or odd // using Bitwise AND operator (&). #include<stdio.h> int main() { int number; //Ask the user for input printf("Enter a number: "); scanf("%d",&number); // Check even or odd using bitwise AND operator (&) if(number & 1){ printf("%d is an Odd number", number); } else{ printf("%d is an Even number", number); } return 0; }
Output1:
Enter a number: 13 13 is an Odd number
Output2:
Enter a number: 14 14 is an Even number
We can make our code reusable by defining a user-defined function. When we define a function, we can call it any time by writing the code only once. It is a good practice to write our code using functions.
Following is the code in C to check even-odd using a user-defined function.
// C program to check a whether number is even or odd // by defining a user defined function. #include <stdio.h> // Define the function void isEvenOrOdd(int number) { //Check the number is even or odd if(number & 1){ printf("%d is an Odd number", number); } else{ printf("%d is an Even number", number); } } int main() { int number; //Ask the user for input printf("Enter a number: "); scanf("%d",&number); //call the user defined function isEvenOrOdd(number); return 0; }
Output:
Enter a number: 71 71 is an Odd number
Thank you for reading this article. We hope all your doubts have been cleared. Happy Coding…!