This C program checks whether the entered number is even or odd. Same algorithm is used to check whether a given number is even or odd.
Prerequisite knowledge:
- A number is even if it is a multiple of 2, and odd if its not.
- Remainder is calculated using the modulus operator (%).
- Basic C programming syntax.
Program: C program to check whether a number is even or odd without using function
#include<stdio.h> int main() { int a; printf("Enter a number: "); scanf("%d",&a); if((a%2)==0) printf("\nEntered number is EVEN!!!\n"); else printf("\nEntered number is ODD!!!\n"); return 0; }
% operator is used to find remainder when first number is divided by the second number.
Output:
Enter a number: 59
Entered number is ODD!!!
Program: C program to check whether a number is even or odd using function
In this program, the number is checked for even/odd using bitwise operator. To write a program without using bitwise operator, follow the next code snippet.
#include<stdio.h> int isEven(int); int main() { int a; printf("Enter a number: "); scanf("%d",&a); if(isEven(a)) printf("\nEntered number is EVEN!!!\n"); else printf("\nEntered number is ODD!!!\n"); return 0; } int isEven(int a) { return !(a & 1); }
Program: Even odd program in C using function – II
#include<stdio.h> int isEven(int); int main() { int a; printf("Enter a number: "); scanf("%d",&a); if(isEven(a)) printf("\nEntered number is EVEN!!!"); else printf("\nEntered number is ODD!!!\n"); return 0; } int isEven(int a) { return (a % 2 == 0); }
Output:
Sample output 1:
Enter a number: 343
Entered number is ODD!!!
Sample output 2:
Enter a number: 434
Entered number is EVEN!!!
If this post helped you, share this with your other programmer friends.