C Program to Check a Number is Even or Odd Number.
Even number: Any
integer (never a fraction) that can be divided exactly by 2 is called Even number.
Example:
2,4,6,8,10,12,14.
Odd number: Any
integer (not a fraction) that cannot be divided exactly by 2 is called Odd number.
Example:
1,3,5,7,9,11,13.
Even and Odd Number Program in C:
To understand this example, you should have the knowledge of
following topics:
- C if Else Statement
- C For loop
- C programming operators
C program to Check Even (or) Odd Number:
#include <stdio.h>
int main()
{
int x;
printf("Enter the value of x = ");
scanf("%d", &x);
if(x % 2 == 0)
printf("%d is even.", x);
else
printf("%d is odd.", x);
return 0;
}
Output:
Enter the value of x = 7
7 is odd.
Explanation:
- First, the entered number is Checked with the condition (x % 2) if the entered value is divided by “ 2 ” the number is Even number if the condition is failed then the Number is Odd Number.In the above condition modulus(%) implies the remainder value.
- Let the value of 'x' entered is 7.
- If (x%2==0) then ‘x’ is an Odd number, else Even. If (7%2==0) then 7 is an Odd number, else Even.
Even and Odd Number Program in C Using Conditional Operator:
C Even Odd number program:
#include <stdio.h>
int main()
{
int x;
printf("Enter the value of x = ");
scanf("%d", &x);
(x % 2 == 0) ?
printf("%d is even.", x) : printf("%d is odd.", x);
return 0;
}
Output:
Enter the value of x = 14.
14 is Even.
Even Odd number program in C using Bitwise operator:
C Even Odd number program:
#include<stdio.h>
int main()
{
int x;
printf("Enter the value of x = ");
scanf("%d",&x);
if ( x & 1)
printf("%d is an odd number.", x);
else
printf("%d is an even number.", x);
return 0;
}
Output:
Enter the value of x = 14
14 is an even number.