- 1C Hello World Program
- 2C Program to Print Your Own Name
- 3C Program to Print an Integer Entered by the User
- 4C Program to Add Two Numbers
- 5C Program to Multiply Two Floating‑Point Numbers
- 6C Program to Print the ASCII Value of a Character
- 7C Program to Swap Two Numbers
- 8C Program to Calculate Fahrenheit to Celsius
- 9C Program to Find the Size of int, float, double and char
- 10C Program - Add Two Complex Numbers
- 11C Program - Find Simple Interest
- 12C Program - Find Compound Interest
- 13C Program - Area And Perimeter Of Rectangle
- 14C Program - Check Whether a Number is Positive, Negative, or Zero
- 15C Program - Check Whether Number is Even or Odd
- 16C Program - Check Whether a Character is Vowel or Consonant
- 17C Program - Find Largest Number Among Three Numbers
- 18C Program - Calculate Sum of Natural Numbers
- 19C Program - Print Alphabets From A to Z Using Loop
- 20C Program - Make a Simple Calculator
- 21C Program - Generate Multiplication Table
- 22C Program - Reverse a Number
- 23C Program - Check whether the input number is a Neon Number
- 24C Program - Find All Factors of a Natural Number
C Program – Check Whether Number is Even or Odd - Modulus & Bitwise
Even or Odd Using Modulus
#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
if (num % 2 == 0) {
printf("%d is even\n", num);
} else {
printf("%d is odd\n", num);
}
return 0;
}
Enter an integer: 9
9 is odd
The modulus operator returns the remainder of a division. An even number yields a remainder of zero when divided by 2; otherwise the number is odd.
Even or Odd Using Bitwise AND
#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
if ((num & 1) == 0) {
printf("%d is even\n", num);
} else {
printf("%d is odd\n", num);
}
return 0;
}
Enter an integer: 12
12 is even
An integer's least significant bit is 0 for even numbers and 1 for odd numbers. Bitwise AND with 1 quickly checks this bit and determines parity.
⬅ Previous TopicC Program - Check Whether a Number is Positive, Negative, or Zero
Next Topic ⮕C Program - Check Whether a Character is Vowel or Consonant
Next Topic ⮕C Program - Check Whether a Character is Vowel or Consonant
Comments
Loading comments...