C Program to Check Whether a Given Number is Even or Odd

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

We are given a number. We need to determine whether the given number is an odd or even number.

Even Numbers: All numbers divisible by 2 are called even numbers. In other words, all numbers that give remainder as 0 on division by 2 are called even numbers.

Odd Numbers: All numbers that are not completely divisble by 2 are called odd numbers. In other words, all numbers that give a non-zero remainder on division by 2 are called odd numbers.

EVEN ODD 1

Introduction

Input Format: The input to our even-odd program in C consists of a single number that we want to categorise as even or odd.

Output Format: Print "Even" (without quotes) if the input number is even. Print "Odd" (without quotes) if the given input number is odd.

Example 1:

This is because 5 on division by 2 gives remainder 1.

Example 2:

This is because 10 on division by 2 gives the remainder 0.

C Program to Check Even or Odd

Using Modulus Operator

  • The first way to code even odd program in C is by using modulus ( % ) operator in C. % Operator produces the remainder of integer division .If a and b are integers then a%b will given us the remainder when a is divided by b.

  • If b divides a completely then the remainder is 0 otherwise we will get remainder in the range [1 to a-1].

  • In this odd program, we will find ** input number % 2 **. If we get 0 as the remainder, it means our number is completely divisible by 2 tand hence is is an even number. And if we get a non-zero remainder, it means our number is not completely divisible by 2; hence, the given number is odd.

Code:

Output:

Time Complexity: O(1)

Auxiliary Space: O(1)

Using Bitwise Operator

Here in this even odd program in C, we will use bitwise operator. If we consider our number in binary then we can easily understand that if our last bit is 1 then our number is odd otherwise it is even. This is because least significant bit of all odd numbers is set.

EVEN ODD 2

EVEN ODD 3

So to check whether our last bit is 1 or not we will do bitwise AND (&) of our given number with 1. If the last bit will be 1, we will get 1 otherwise we will get 0.

Truth table for AND operator EVEN ODD 4

For Example

Code

Output:

Time Complexity: O(1)

Auxiliary Space: O(1)

Program to Check Even or Odd Using the Ternary Operator

The ternary operator, also known as the conditional operator, is similar to the if-else statement as it follows the same method, but the conditional operator takes up less space and allows you to write if-else statements in the quickest possible time.

Syntax of Ternary operator:

(expression_1) ? (expression_2) : (expression_3);

expression_1 refers to the condition to be checked . If expression 1 evaluates to true then expression_2 is executed else expression_3 is executed.

It's similar to :

EVEN ODD 5

EVEN ODD 6

Here in this odd even program , we check whether input number % 2 is 0 or not . Then using conditional operator output the desired result.

Code:

Output:

Time Complexity: O(1)

Auxiliary Space: O(1)

C Program to Check Odd or Even Without Using Bitwise Or Modulus Operator

Input : A single number n.

In this odd-even program, we will check whether the given input is odd or even by using a loop. We will initialise a variable x to 1 initially, and then we will run our loop n a number of times, and each time, we will complement our variable x (meaning if it was 1, then it will now become 0, and if it was 0, it will become 1 now).

Now, if our variable x is 1 at the end of the loop, then we can say that our number is even; otherwise, our number is odd.

Code :

Output:

Time Complexity: O(n)

Auxiliary Space: O(1)

Conclusion

  • Even Numbers: All those numbers that are completely divisible by 2 are called even numbers.

  • Odd Numbers : All those numbers that are not completely divisble by 2 are called odd numbers.

  • a%b returns the remainder when a is divisble by b. If b divides a completely we get remainder as 0. Otherwise we will get a number between [1 to a-1].

  • The last bit of a number in binary representation decides whether the number is odd or even. If the last bit is 1, then the number odd and if the last bit is 0 then the number is even.

  • Ternary Operator also known as conditional operator is similar to if-else statements.

  • Syntax of Ternary Operator is (expression_1) ? (expression_2) : (expression_3);