If any number is exactly divisible by 2 then itโs an even number else itโs an odd number.
In this tutorial weโll see how to check whether the provided number is even or odd number using Modulo(%).
Flowchart
Flowchart for odd even
Note
- The remainder calculated with
%(Modulo) on dividing by 2, will always be0for all even numbers. - Whereas the remainder on dividing by 2, will always be
1for all odd numbers.
Code
int main()
{
int a = 256;
int rem = a % 2;
printf("\nA: %d", a);
printf("\nRemainder: %d", rem);
if(rem == 0)
{
printf("\n%d is an even number", a);
}
else
{
printf("\n%d is an odd number", a);
}
/*
Divided by 2
12 % 2 -> 0
13 % 2 -> 1
14 % 2 -> 0
15 % 2 -> 1
16 % 2 -> 0
17 % 2 -> 1
18 % 2 -> 0
19 % 2 -> 1
20 % 2 -> 0
21 % 2 -> 1
22 % 2 -> 0
*/
printf("\n");
return 0;
}
Output
256 is an even number
Happy ๐ coding
Related Articles
Deepen your understanding with these curated continuations.
Print Upside Down Triangle Pattern in C
Master C pattern programming with this tutorial on printing an upside-down triangle. Includes fuzzy logic and nested loop implementations for developers.
Sum of Series Between Two Numbers Using For Loop in C
Learn how to calculate the sum of a series between two integers in C using a for loop. Detailed tutorial with logic explanation and working code.
Calculate Factorial in C Using a For Loop
Learn how to calculate the factorial of an integer in C using a for loop. Includes a step-by-step algorithm and complete working code examples.