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

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

Problem Description

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

Problem Solution

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.

Method 1: (Using if-else and Modulus Operator)

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

advertisement
advertisement
Program/Source Code

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.

  1. /*
  2.  * C Program to check whether a number is even or odd 
  3.  * using if-else statement and modulus operator
  4.  */
  5.  
  6. #include <stdio.h>
  7. void main() 
  8. {
  9.     int n;
  10.     printf("Enter a number: ");
  11.     scanf("%d", &n);
  12.  
  13.     if(n % 2 == 0)
  14.         printf("%d is even number.", n);
  15.     else
  16.         printf("%d is odd number.", n);
  17. }
Program Explanation

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.

Runtime Test Cases

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”.

advertisement
Enter a number: 15 
15 is odd number.

Method 2: (Using Ternary Operator)

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

Program/Source Code

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.

  1. /*
  2.  * C Program to check whether a number is even or odd using ternary operator
  3.  */
  4.  
  5. #include <stdio.h>
  6. void main()
  7. {
  8.     int n;
  9.     printf("Enter an integer: ");
  10.     scanf("%d", &n);
  11.  
  12.     (n % 2 == 0) ? printf("%d is even number.", n) : printf("%d is odd number.", n);
  13. }
Program Explanation

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.

advertisement

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.

Runtime Test Cases

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.

Method 3: (Using Bitwise Operator)

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

Program/Source Code

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.

  1. /*
  2.  * C Program to check whether a number is even or odd using bitwise operator
  3.  */
  4.  
  5. #include <stdio.h>
  6. void main() 
  7. {
  8.     int n;
  9.     printf("Enter a number: ");
  10.     scanf("%d", &n);
  11.  
  12.     if(n & 1==1)
  13.         printf("%d is odd.", n);
  14.     else
  15.         printf("%d is even.", n);
  16. }
Program Explanation

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.

Runtime Test Cases

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]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.