**Even Number:**

A number is said to be an **even number** if it is completely divisible by 2.

In other words, if a number is divided by 2 and leaves a remainder of 0, then it is said to be an even number.

**Example:** 36, 24

**Odd Number:**

A number is said to be an **odd number** if it is not completely divisible by 2.

In other words, if a number is divided by 2 and the remainder is 1, it is said to be an odd number.

**Example:** 21, 15

Write a C Program to check whether a given number is even or odd.

1. Take the integer to be checked as input.

2. Find the remainder of the integer by dividing it by 2.

3. Use if, else statement to check whether the remainder is equal to zero or not.

4. Print the output and exit.

There are several ways to check whether a given number is even or odd in C language. Let’s take a detailed look at all the approaches to check whether a given number is even or odd.

- Check Even or Odd in C using if-else and Modulus Operator
- Check Even or Odd in C using Ternary Operator
- Check Even or Odd in C using Bitwise Operator

In this approach, we check whether a number is even or odd using if-else statement and modulus operator.

Here is source code of the C program to check whether a given integer is even or odd using if-else statement and modulus operator. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

`/*`

`* C Program to check whether a number is even or odd`

`* using if-else statement and modulus operator`

`*/`

`#include <stdio.h>`

void main()

`{`

int n;

printf("Enter a number: ");

scanf("%d", &n);

if(n % 2 == 0)

printf("%d is even number.", n);

`else`

printf("%d is odd number.", n);

`}`

1. In this C program, we are reading a number **n** from user as input.

2. After that we divide the number(**n**) by 2 and check if the remainder is 0 or 1.

3. If the remainder is 0, we print that it is an even number

4. If the remainder is 1, we print that it as an odd number.

**Example:**

Consider an example, let the number entered by the user is 22. We perform **n%2** i.e., **22%2 = 0**. Therefore, we print 22 is even.

**Time Complexity: O(1)**

The above program for checking whether a number is even or odd has a time complexity of O(1), as we are using only if-else statement.

**Space Complexity: O(1)**

In the above program, space complexity is O(1) as no extra variable has been taken to store the values in the memory. All the variables initialized takes a constant O(1) space.

**Testcase 1:** Here is the runtime output of the C program to check whether a number is even or odd when the user enters number = “22”.

Enter a number: 22 22 is even number.

**Testcase 2:** Here is the runtime output of the C program to check whether a number is even or odd when the user enters number = “15”.

Enter a number: 15 15 is odd number.

In this approach, we check whether a number is even or odd using ternary operator.

Here is source code of the C program to check whether a number is even or odd using ternary operator. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

`/*`

`* C Program to check whether a number is even or odd using ternary operator`

`*/`

`#include <stdio.h>`

void main()

`{`

int n;

printf("Enter an integer: ");

scanf("%d", &n);

(n % 2 == 0) ? printf("%d is even number.", n) : printf("%d is odd number.", n);

`}`

1. In this C program, we are reading a number **n** from user as input.

2. Using the ternary operator, we find whether the number is even or odd.

3. We check whether **n%2 = 0** or not.

4. If the condition is true, then the first statement after question mark gets executed else the second statement gets executed.

**Example:**

Consider an example, let the number entered by the user is **37**. We check the condition **(n%2==0)** i.e., **37%2** which is equal to **1**. Since the condition is **false**, we execute the second statement after the question mark and print **37 is odd number**.

**Time Complexity: O(1)**

The above program for checking whether a number is even or odd has a time complexity of O(1), as we are using only ternary operator which has a time complexity of O(1).

**Space Complexity: O(1)**

In the above program, space complexity is O(1) as no extra variable has been taken to store the values in the memory. All the variables initialized takes a constant O(1) space.

**Testcase 1:** Here is the runtime output of the C program to check whether a number is even or odd when the user enters number = “37”.

Enter a number: 37 37 is odd number.

**Testcase 2:** Here is the runtime output of the C program to check whether a number is even or odd when the user enters number = “18”.

Enter a number: 18 18 is even number.

In this approach, we check whether a number is even or odd using bitwise operator.

Here is source code of the C program to check whether a number is even or odd using bitwise operator. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

`/*`

`* C Program to check whether a number is even or odd using bitwise operator`

`*/`

`#include <stdio.h>`

void main()

`{`

int n;

printf("Enter a number: ");

scanf("%d", &n);

if(n & 1==1)

printf("%d is odd.", n);

`else`

printf("%d is even.", n);

`}`

1. In this C program, we are reading a number **n** from user as input.

2. Using the bitwise operator, we find whether the number is even or odd.

3. We check whether **n&1 = 1** or not.

4. If the condition is true, then we print **n is odd number** else we print **n is even number**.

**Example:**

Consider an example, let the number entered by the user is **7**. We check the condition (**n&1==1**) i.e., **7&1**. Bitwise operator works with binary numbers. Therefore it will convert 7 and 1 to binary and then perform the operation.

7 in binary = 00000111

1 in binary = 00000001

Value after **bitwise & = 00000001**

We, get the result as **1**, therefore **7 is odd number**.

**Time Complexity: O(1)**

The above program for checking whether a number is even or odd has a time complexity of O(1), as we are using only bitwise operator which has a time complexity of O(1).

**Space Complexity: O(1)**

In the above program, space complexity is O(1) as no extra variable has been taken to store the values in the memory. All the variables initialized takes a constant O(1) space.

**Testcase 1:** Here is the runtime output of the C program to check whether a number is even or odd when the user enters number = “7”.

Enter a number: 7 7 is odd number.

**Testcase 2:** Here is the runtime output of the C program to check whether a number is even or odd when the user enters number = “22”.

Enter a number: 22 22 is even number.

To practice programs on every topic in C, please visit “Programming Examples in C”, “Data Structures in C” and “Algorithms in C”.

**If you find any mistake above, kindly email to [email protected]**

**Related Posts:**

- Watch Advanced C Programming Videos
- Practice BCA MCQs
- Practice Computer Science MCQs
- Check C Books
- Apply for Computer Science Internship