Q6. Write a code to check whether the number is even or odd
Here you will find an algorithm and program to check whether a number is an even number or an odd number. we
know that :-
Even Number :- Even numbers are the numbers which are divisible by 2. For Example :- 2, 4, 6, …
Odd Number :- Odd numbers are the numbers which are not divisible by 2. For Example :- 1, 3, 5, …
Now let us understand what logic we need to use in this –
As we know that, to know number is even or odd we used to check its divisibility with 2, so here we will check the remainder we get on dividing the input number with 2 if we get remainder as 0 then it is divisible by 2 and hence it is even number else it is odd number.
For Example :-
Input : 12
Output : Even Number
Explanation : As we can see that the number given in input, which is 12, on dividing with 2 we get remainder 0 therefore 12 is Even Number.
Algorithm to check given number is even or odd
\\Algorithm to check number is even or odd.
START
Step 1: [ Take Input ] Read: Number N
Step 2: Check: If N%2 == 0 Then
Print : N is an Even Number.
Else
Print : N is an Odd Number.
STOP
Code to check given number is even or odd
\\C program to check whether the given number is even or odd.
#include <stdio.h>
int main() {
int N=12;
// True if num is perfectly divisible by 2
if(N % 2 == 0)
printf(” %d is Even number”,N);
else
printf(” %d is Odd number”,N);
return 0;
}
\\C++ program to check whether the given number is even or odd.
#include <iostream>
using namespace std;
int main()
{
int N=12;
if ( N % 2 == 0)
cout <<“The number “<< N << ” is Even number.”;
else
cout <<“The number “<< N << ” is Even number.”;
return 0;
}
\\Java program to check whether the given number is even or odd.
import java.util.Scanner;
class LFC
{
public static void main(String args[])
{
int N=12;
/* If number is divisible by 2 then it’s an even number
* else odd number*/
if ( N % 2 == 0 )
System.out.println(” “+N+” is Even number”);
else
System.out.println(” “+N+” is Odd number”);
}
}
\\Python program to check whether the given number is even or odd.
N=12
if (N % 2) == 0:
print(“{0} is Even number”.format(N))
else:
print(“{0} is Odd number”.format(N))
\\PHP program to check whether the number is even or odd.
$N=12;
if($N%2==0)
{
echo ” $N is Even number”;
}
else
{
echo “$N is Odd number”;
}
Output
12 is an Even number
Recommended Programs
Program to find factorial of a number
Program to count number of digits in a number