In this example, we will learn to check if the number is even or odd.
A number is even if it is divisible by 2 whereas a number is odd if it is not divisible by 2.
For example:
Even numbers are; -4, -6, 0, 2, 4, 100, -200, etc. Odd numbers are: 1, 11, -21, 225, etc.
Some of you might say odd and even numbers cannot be negative. This is a controversial topic so, any of the theories you follow is correct for now. While you can check this article/post from wolfram which says non-negative numbers can also be odd or even.
The main Idea
Let’s see how we will check if the number is even or odd. We know that even numbers are divisible by 2 and odd numbers are not divisible. This means that whenever we divide even numbers by 2 we get a remainder of 0. However, we will get the remainder of 1 (or -1 if the number is negative) if we divide odd numbers by1.
Example:
4 / 2 => reminder = 0, so number is even 5 / 2 => remainder = 1, so number is odd
In C, we use the “%” operator to find the remainder of a number. Then,
4 % 2 = 0 /* number is even */ 5 % 2 = 1 /* number is odd */
Algorithm to check if the number is odd or even
We will use the variable num to store the value of the number. Algorithm is given as follows,
Step 1: Start. Step 2: Take an integer as input in variable num. Step 3: Divide the num by 2. Step 4: If the remainder is 0 then num is even. Step 5: If the remainder is 1 or -1 then num is odd. Step 6: End.
In short,
Start, if num % 2 == 0 then num is even if num % 2 == 0 then num is odd End.
Also Read: C program to Check whether a number is positive, negative or zero
Program to check if the number is even or odd using if
#include<stdio.h> int main(){ int num; printf("Enter the number: "); scanf("%d", &num); /* If the remainder is 0 then even else odd */ if(num % 2 == 0){ printf("%d is even.", num); } if(num % 2 == 1 || num % 2 == -1){ printf("%d is odd.", num); } return 0; }
Output
Enter the number: 80 80 is even.
Enter the number: -5 -5 is odd.
Explanation
- We are taking an integer number as input in the variable
num
. - After that, to check if the number is odd or even we used
if
statements.
if(num % 2 == 0){ . . . } if(num % 2 == 1 || num % 2 == -1){ . . . }
In the first if
block we checked if the number is even. The condition num % 2 == 0
means that whenever a num
is divided by 2 it should give 0 which is the required condition for a number to be even. It works for both negative and positive numbers.
Similarly, in the next if
block we check if the number is odd. The condition num % 2 == 1
and num % 2 == -1
means that when we divide a number by 2 it gives remainder of either 1 or -1. The remainder is 1 if the number is positive and -1 if the number is negative. Also, there is ||
symbol which is the symbol for OR
. This ensures if either of the condition is true the if
block executes.
- Finally, in the inside the
if
block we print the output accordingly.
Program using if…else
#include<stdio.h> int main(){ int num; printf("Enter the number: "); scanf("%d", &num); /* If the remainder is 0 then even else odd */ if(num % 2 == 0){ printf("%d is even.", num); } else { printf("%d is odd.", num); } return 0; }
Output
Enter the number: 20 20 is even.
Enter the number: -255 -255 is odd.
Explanation
While using multiple if
blocks even if the condition in first if block satisfies other succeeding if blocks will be executed. So to eliminate this we can use either if...else
or else if
statements.
We used if...else
to check the condition because else if
couldn’t be used since we have only 2 conditions.
if(num %2 == 0){ . . . } else { . . . }
For the condition part, we checked the remainder of the num when it is divided by 2.
i.e. num % 2
If remainder is 0 then num is even else odd.
20 % 2 = 0 /* so 20 is even */ -255 %2 = -1 != 0 /* so, -255 is odd */
More Examples