C Program to check whether the number is even or odd.
Last updated on July 27, 2020
What are Even or Odd numbers: #
Even Number: A number which is evenly divisible (i.e leaves no remainder) by 2
is called an even number.
Odd Number: A number which is not evenly divisible by 2
is called an odd number.
The following is a C program to check whether a number is even or odd.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | /*************************************************************
* Program to check whether the entered number is even or odd.
**************************************************************/
#include<stdio.h> // include stdio.h
int main()
{
int n;
printf("Enter a number: ");
scanf("%d", &n);
if(n % 2 == 0)
{
printf("%d is even", n);
}
else
{
printf("%d is odd", n);
}
return 0;
}
|
Expected Output: 1st run:
1 2 | Enter a number: 18
18 is even
|
2nd run:
1 2 | Enter a number: 5555
5555 is odd
|
How it works #
A number is even if it is evenly divisible by 2. To check for even number we simply use remainder operator %
. If the number is even then remainder operator will return 0
, otherwise, a non-zero value. For example:
1 2 | 4 % 2 = 0 => 4 is even
11 % 2 = 1 => 11 is odd
|
Related Programs
- C Program to find the sum of natural numbers upto N terms
- C Program to reverse the digits of a number
- C Program to check whether a year is a leap year
- C Program to count number of digits in a number
- C Program to generate Fibonacci sequence
Load Comments