Table of Content
An even number is a number that is completely divisible by 2. An odd number is a number that is not completely divisible by 2. If you divide an odd number then the remainder will always 1.
Example:
Odd numbers : 1,3,5,7,9,11,13,15,17,19
Even numbers: 0,2,4,6,8,10,12,14,16,18,20
Pre-requisite Programming concepts
- Input output operations in C, C++ and Java.
- % opeartor ( it performs division operation and assigns the remainder )
- Data-type in c, c++ and java.
Logic for Checking a Number is Odd or Even
- Input the number.
- Divide the number by 2 if the remainder is equal to 0 then it is an even number otherwise
that number is odd.
C Program to Check Even or Odd Number
#include<stdio.h>
int main(int argc, char const *argv[])
{
//scan a number to check even or odd
int number;
printf("\nOdd or even program in c\n");
scanf("%d",&number);
/*If the number is competely divisible by 2.
then that number is even number.
otherwise that number is odd number.
*/
if( number % 2 == 0 )
{
printf("%d is even number\n",number);
}
else
{
printf("%d is odd number\n",number);
}
return 0;
}
Output
Code Explanation
First print get the number from the user using scanf()
function. Using % check for the remainder that is 1 or 0. If the remainder is zero then print the number is even or print the number is odd number using printf()
function.
People also ask
How do you check whether a number is even or odd in C?
Find the remainder after dividing by 2 using % operator. If the remainder is 0 then the number is even or the number is odd.