Here's a C program to find whether a given number is even or odd with output and proper explanation. This program uses if condition and C modulus to determine whether a number is even or odd.
# include <stdio.h> # include <conio.h> void main() { int num ; clrscr() ; printf("Enter a number : ") ; scanf("%d", &num) ; if(num % 2 == 0) printf("\nThe given number is even") ; else printf("\nThe given number is odd") ; getch() ; }
Output of above program is
Enter a number : 15
The given number is odd
Explanation of above program
Note: In C, the symbol % denotes modulus. In other words, it returns remainder e.g. 14 % 5 will return 4 i.e. the remainder after dividing 14 by 5.
This program is the most basic and simplest program one can get in C. Here, first we ask the user to enter a number and store it in the integer variable num. Then, we calculate modulo 2 of the entered number. That means whenever you divide a number by 2, if the number is even its remainder will always be zero. This can be easily checked with an If-else condition.
We can also check whether a number if odd and even using switch statement in C.