🔀⚡Beginner
Check Whether Number is Even or Odd
Determine if number is even or odd
A number is even if it is divisible by 2 (remainder = 0), otherwise it is odd. We use the modulus operator (%) to find the remainder.
📚 What You Will Learn
- •Using the modulus operator (%)
- •Simple
if-elsedecision making - •Understanding divisibility
C Program to Check Even or Odd
even_odd.c
C
1#include <stdio.h>23int main() {4 int num;5 6 printf("Enter an integer: ");7 scanf("%d", &num);8 9 // Check if remainder is 0 when divided by 210 if (num % 2 == 0) {11 printf("%d is Even\n", num);12 } else {13 printf("%d is Odd\n", num);14 }15 16 return 0;17}Output
Enter an integer: 7
7 is Odd
Understanding the Modulus Operator (%)
The % operator returns the remainder after division:
10 % 2= 0
Even ✓
7 % 2= 1
Odd
0 % 2= 0
Even ✓
-4 % 2= 0
Even ✓
Step-by-Step Execution:
1
Input
User enters 7
2
Calculate Remainder
7 % 2 = 1 (7 = 2×3 + 1)
3
Check Condition
1 == 0 is FALSE
4
Execute else
Prints "7 is Odd"
Alternative Methods
Using Bitwise AND Operator
main.c
C
1// Last bit of odd numbers is always 12if (num & 1)3 printf("Odd");4else5 printf("Even");Faster method! Checks the least significant bit (1 for odd, 0 for even).
Using Ternary Operator
main.c
C
1printf("%d is %s\n", num, (num % 2 == 0) ? "Even" : "Odd");📝 Key Takeaways
✓
% gives remainder after division✓Even:
num % 2 == 0✓Odd:
num % 2 != 0✓Works for negative numbers too!
Related Examples
Want to Learn More?
Explore our comprehensive tutorials for in-depth explanations of C programming concepts.
Browse Tutorials