Q:-Write a C program to check whether a given number is even or odd. If a number is exactly divisible by 2 then its an even number else it is an odd number. How to check whether a number is even or odd using if else in C program. C Program to input a number from user and check whether the given number is even or odd. Logic to check even and odd number using if…else in C programming.
#include<stdio.h> #include<conio.h> void main() { int num; clrscr(); printf("Enter an integer number: "); scanf("%d",&num); /*If number is divisible by 2 then number is EVEN otherwise number is ODD*/ if(num%2==0) printf("%d is an EVEN number.",num); else printf("%d is an ODD number.",num); getch(); }
In the program, the integer entered by the user is stored in the variable num.
Then, whether num is perfectly divisible by 2 or not is checked using the modulus % operator.
If the number is perfectly divisible by 2, test expression number%2 == 0 evaluates to 1 (true). This means the number is even.
However, if the test expression evaluates to 0 (false), the number is odd.