Skip to main content

Let’s understand how to check if a number is odd or even in C Programming. I am referring to an awesome book called “Let us C” by “Yashwant Kanetkar” and I will post solutions to the questions asked in exercises. Please come back for more programs. If you have any problems with the program or need any assistance, don’t hesitate to write to us. We try to answer as quickly as possible.

Question:

Write a program to check whether the given number is odd or even. Use both the modulus and bitwise operators.

Solution to check whether a number is even or odd in C Programming:

You are going to enjoy this program because it is super simple. We will write two programs to achieve this problem. In the first program, we will use the modulus (%) operator and in the second one, we will use the bitwise operator. We have already used the modulus operator in C Program to Calculate the Sum of Digits.

Quick Recap: A modulus operator finds the remainder after dividing a number by another. ‘%’ symbol is used in the program as the modulo operator.

A bitwise operator performs certain operations on the bit level. Eg: The unary ‘++’ operator increments the number by one.

The algorithm for the program is quite similar for both the operators. First, we ask the user to input a number and then we use either the modulo or the bitwise operator to check if the number is even or odd. Then, we display the desired output.

Download the source code:

Download Programs

Program to check an even or odd number in C using the modulus operator:

// C Program to check if the number is Odd or Even using the modulus operator.

#include<stdio.h>

int main()
{
  
   int number;
 
   printf("Enter an integer: ");
   scanf("%d",&number);
 
   // The Modulo operator returns the remainder
   if ( number%2 == 0 )
      printf("%d is an even number", number);
   else
      printf("%d is an odd number", number);
 
   return 0;
}

Output

Enter an integer – 5

5 is an odd number

Program to check an even or odd number in C using the modulus operator:

// C Program to check if the number is Odd or Even using the bitwise operator.

#include<stdio.h>

int main()
{
   int number;

   printf("Enter an integer: ");
   scanf("%d",&number);

   if ( number & 1)
      printf("%d is an odd number", number);
   else
      printf("%d is an even number", number);

   return 0;
}

Output

Enter an integer – 22

22 is an even number

Download the source code:

Please don’t forget to subscribe to us before you hit the download button! It helps us grow and provide more content.

Download Programs

Read Also:

C Program to Calculate the Sum of Digits.

Multiplying two numbers in C

Print ASCII Values Using While Loop in C Program

How to Calculate Simple Interest in C Programming

Harsh Shah

A highly motivated individual with a hunger to learn and share as much as I can about everything. Masters in Interaction Design with a Bachelors in Computer Science. I have worked with clients across the globe on various design and tech projects. I believe in giving back and that's how ReadMeNow was founded.

Leave a Reply