C Program to Check Whether a Given Number is Even or Odd
Even and odd numbers are the most fundamental concepts of number theory. Even numbers are those numbers, which give the remainder zero when divided by two. Odd numbers are those numbers, which give the remainder one when divided by two. Using these basic calculations, we can easily detect whether a given number is even or odd. We can have two approaches to solve this problem.
- Using the modulus operator(%)
- Using the bitwise operators
Let us solve the problem with C code.
Using The Modulus Operator (%)
In C, we have an operator named modulus operator (%), which returns the remainder of the operands. For example, if we have an expression a%b (a modulo b), this returns the remainder when a is divided by b. We will first write an algorithm for this approach.
Algorithm:
- Start
- Declare an integer variable ‘num’.
- Take user input for a number and store it in ‘num’.
- Check if the num%2 is zero.
- If num%2 equals zero then print that number is even.
- If num%2 not equals zero then print that number is odd.
- End.
Let us write a program based on the above algorithm.
Code:
#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
// Check if the number is even
if (num % 2 == 0) {
printf("%d is even.\n", num);
}
// Check if the number is odd
else {
printf("%d is odd.\n", num);
}
return 0;
}
Output:
Let us see an alternate program using the same approach. Here we will use the ternary operator, which is a shorthand for if else statement. Ternary operator is denoted by ‘?:’ sign. It evaluates a condition and returns a value on the basis of whether the condition is true or false.
Syntax for the ternary operator is as follows:
(condition) ? true_value : false_value;
If the condition is true then true_value expression is executed. If the condition is false then false_value expression is executed.
Code:
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
num % 2 == 0 ? printf("%d is even", num) : printf("%d is odd", num);
return 0;
}
Output:
We have seen two programs to check whether a given number is even or odd. Here, we have used the modulus operator(%) approach, which returns the remainder of two operands.
Now, we will go with the next approach to check whether a given number is even or odd.
Using Bitwise Operators
In C, bitwise operators are those operators that operate on individual bits of a number. Here we will use the AND(&) operator for checking even or odd. The bitwise operator AND(&) sets each bit to one if both the bits are one. In binary, even numbers end with 0, and odd numbers end with 1. Using this fact, we can determine whether a number is even or odd. We will use AND(&) operator with 1, if the result is 0 then the number is even, and if the result is 1 then the number is odd. Let us develop an algorithm using the given calculations.
Algorithm:
- Start
- Declare an integer variable ‘num’.
- Take user input and store it in ‘num’.
- Check the last bit of the ‘num’ using bitwise AND(&) operator with 1.
- If the result is 1 then print odd.
- If the result is 0 then print even.
- End
Now, let us write a C program for the above algorithm.
Code:
#include <stdio.h>
int main()
{
int num;
printf("Enter a number: ");
scanf("%d", &num);
if(num & 1) // using bitwise AND operator with 1
{
printf("%d is odd.", num);
}
else
{
printf("%d is even.", num);
}
return 0;
}
Here we are using bitwise AND(&) operator with 1. If the result is 1 then ‘num’ is odd, else it is even.
Output:
Conclusion
We have seen both the approaches to check whether a number is even or odd. Even or odd checking program is an essential practice to understand the basics of a programming language. Here with this program, we have learnt the concept of if else statements, ternary operator and the bitwise AND(&) operator. These types of problems are intended to develop a deep understanding of basic concepts of a programming language.