C Program to Check Whether a Number is Even or Odd Using If-Else
In the following program, we will check if the entered number is completely divisible by 2. To check this, we will use Modulus Operator(%). This operator will return the remainder of the division. If the remainder is zero that means the number is completely divisible by 2 and is even number else it will be an odd number.
#include<stdio.h> #include<conio.h> void main() { int n; clrscr(); printf("\n Enter Any Number : "); scanf("%d",&n); if(n%2==0) { printf("\n %d is Even number",n); } else { printf("\n %d is Odd number",n); } getch(); }
Output :
Also Check, How to Print Even and Odd Number List
C Program To Find Even Or Odd Using Switch Case
#include<stdio.h> #include<conio.h> void main() { int n,r; clrscr(); printf("\n Enter Any Number : "); scanf("%d",&n); r=n%2; switch(r) { case 0 : printf("\n %d is Even Number",n); break; default: printf("\n %d is Odd Number ",n); } getch(); }
In the above program, We have stored the remainder in the variable 'r'and checked whether that is zero or not using the switch case. If it is zero the entered number is even, if not the number is odd.
C Program To Check Even Or Odd Using Conditional Operator
#include<stdio.h> #include<conio.h> void main() { int n; clrscr(); printf("\n Enter Any Number : "); scanf("%d",&n); n%2 == 0 ? printf("\n %d is Even Number",n) : printf("\n %d is Odd Number",n); getch(); }
C Program To Find Even Or Odd Number Using Function
#include<stdio.h> #include<conio.h> int evenodd(int x); void main() { int n; clrscr(); printf("\n Enter Any Number : "); scanf("%d",&n); evenodd(n); getch(); } evenodd(int x) { if(x%2==0) printf("\n %d is Even Number",x); else printf("\n %d is Odd Number",x); return 0; }
Thus, we can easily find out the given number is even or odd in c programming by various methods.