C Program to check whether the Entered number is even or odd ?

If a number is exactly divisible by 2 then its an even number else it is an odd number. In this article we have shared two ways to check whether the input number is even or odd. 

1). Using Modulus operator(%)

 2). Using Bitwise and operator. (&)

Program 1: Using Modulus operator

/* Program to check whether the input integer number 
 * is even or odd using the modulus operator (%)
 */
#include<stdio.h>
int main()
{
   // This variable is to store the input number 
   int num;
 
   printf("Enter an integer: ");
   scanf("%d",&num);
 
   // Modulus (%) returns remainder
   if ( num%2 == 0 )
      printf("%d is an even number", num);
   else
      printf("%d is an odd number", num);
 
   return 0;
}

Output:

Enter an integer: 10
10 is an even number



Program 2: Using Bitwise operator

/* Program to check if number is even or odd
 * using bitwise operator
 */
#include<stdio.h>
 
int main()
{
   int n;
 
   printf("Enter an integer: ");
   scanf("%d",&n);
 
   if ( n & 1)
      printf("%d is an odd number", n);
   else
      printf("%d is an even number", n);
 
   return 0;
}

Output:

Enter an integer: 11
11 is an odd number


Recommended Book Must Buy :

To improve your skill and Knowledge you must read Let Us C 16TH EDITION  because this book is totally recommended for beginners and you can improve your skills by attempting example and question inside it 

Note : If you want this book with solution . what you have to do is to click here .



Post a Comment

0 Comments