Hey there!
Welcome to ClearUrDoubt.com.
In this post, we will look at a C program to find whether a given number is even or odd.
Even Number: A number which is divisible by 2
Odd Number: A number which is not divisible by 2
To find whether a number is even or odd, we need to check the remainder when we divide the given number with 2.
This can be achieved using Modulus operator(“%”) in C. This operator gives the remainder of the division expression.
Eg.
10 % 3 = 1
11 % 3 = 2
12 % 3 = 0
Here is the C program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
/* C program to find if the given number is an even or odd number */ #include<stdio.h> int main() { int num; printf("Enter a number: "); scanf("%d", &num); if(num % 2 == 0) { printf("%d is an even number.", num); } else { printf("%d is an odd number.", num); } return 0; } |
Output:
Happy Learning! 🙂
Please leave a reply in case of any queries.