C Program to check if the number is odd or even

In this C Programming example, we will implement the Program to check if the number is odd or even using conditional statements, relational operator and Bitwise Operator.

1. Odd or Even number

Given an integer (say n), we need to check whether the integer entered by the user is Odd or Even. If the number entered is found to be Odd than print “Number is Odd” otherwise, print “Number is Even”.

Input : 4
Output : even

Input : 3
Output : odd

Helpful topics to understand this program better are


2. C Program to check if the number is Odd or Even

Let’s discuss the execution(kind of pseudo-code) for the program to check if the number is Odd or Even in C.

2.1. Using if-else

In this section, we will discuss how to evaluate if the integer entered by the user is Odd or Even and implement the C program using if-else.

  1. The user enters the number which is stored in the variable int number.
  2. We use the modulus (%) operator to check if the number is divisible by 2.
  3. If the number is perfectly divisible by 2 then the condition if(number%2==0) holds true else it is false and the proper output is printed on the console.
#include <stdio.h>

int number;

void oddOrEven() {
  if (number % 2 == 0) {
    printf("%d is even.", number);
  } else {
    printf("%d is odd.", number);
  }
}

int main() {
  printf("Enter the value: ");
  scanf("%d", &number);
  oddOrEven();
  return 0;
}
Enter the value: 3
3 is odd.

2.2. Using Bitwise Operator

Another approach to checck whether number is odd or even is to use a bitwise operator.

The approach to use the Bitwise operator is to check whether the last bit of the given number is 1 or not.

  1. To check whether the last bit is 1 find the value of if(number & 1).
  2. If the result is found to be 1, then print “Number is Odd”, otherwise, print “Number is Even”.
Binary representation of 5 : 0101
Binary representation of 1 : 0001
_____________________________________
"The result of the Bitwise AND(&) Operation is 1, 
if both the bits have the value as 1. 
Otherwise, the result is always 0."

Note: We will just update the method OddOrEven as the main method remains the same.

void oddOrEven() {
  if (number & 1) {
    printf("Number is Odd");
  } else {
    printf("Number is Even");
  }
}
Enter the value: 6
Number is Even

3. Conclusion

In this C Programming example, we have discussed how to check if the number entered by the user is Odd or Even using conditional statement, relational operator, and Bitwise Operator.


Helpful Links

Please follow C Programming tutorials or the menu in the sidebar for the complete tutorial series.

Also for the example C programs please refer to C Programming Examples.

All examples are hosted on Github.


Recommended Books


An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding

Do not forget to share and Subscribe.

Happy coding!! ?

Recommended -

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Index