C program to check whether a number is even or odd
In this article, you will learn how to write different programs in C to check whether a number is odd or even. To check whether a number is odd or even, either you can ask a user to enter a number or you can provide a list from which the program can check whether the number is odd or even. Such type of program is generally asked in programming interviews or in the examinations.
A number is even if it is perfectly divisible by 2, and if the remainder is not zero, the number is odd.
Using Modulus operator
To check whether a given number is EVEN or ODD, we are checking modulus by dividing the number by 2. If the modulus is 0, then it will be completely divisible by 2, hence the number will be EVEN, otherwise it will be ODD.
#include <stdio.h>
int main()
{
int num;
printf("Please enter a number : ");
scanf("%d",&num);
// Using modulus operator
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 : 45
45 is an odd number.
Output 2 -
Please enter a number : 52
52 is an even number.
Using Ternary operator
The ternary operator is an operator that exists in some programming languages, which takes three operands rather than the typical one or two that most operators use. Here, we have used the ternary operator to check the odd even number using C -
#include <stdio.h>
int main() {
int num;
printf("Please enter a number : ");
scanf("%d", &num);
(num % 2 == 0) ? printf("%d is even number.", num) : printf("%d is odd number.", num);
return 0;
}
Output
Please enter a number : 46
46 is even number.
Using Bitwise Operator
Here, we have checked the given number is odd and even using Bitwise & operator. In this approach, we have checked whether the last bit of the given number N is 1 or not. If the result of (N & 1) is 1, then print "Odd". Otherwise, print "Even".
#include<stdio.h>
int main()
{
int num;
printf("Please enter a 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 of the above code :
Please enter a number : 92
92 is an even number
Related Articles
Prime factors of a number in cArmstrong number program in c
Write a program to check leap year in c
C program to find area of rectangle
C program to convert celsius to fahrenheit
Fibonacci series program in C using recursion
Write a program to find area of circle in C
C program to find greatest of three numbers
C program for addition of two numbers
C program to calculate compound interest
C program to find the ASCII value of a character
C program to convert Decimal to Octal
C program to convert decimal to binary
Write a C program to calculate Simple Interest
C program to check whether a number is even or odd
C program to reverse a number
C program to check palindrome number
C program to check whether an alphabet is a vowel or consonant
Program to find square root of a number in C
C program to check whether a number is positive or negative