C

C Program To Determine Whether A Number Is Even Or Odd

If a number is divisible by two, it is an even number; otherwise, it is an odd number. In this article, we discussed two methods (two C programs) for determining whether an input number is even or odd. 1) Applying the Modulus operator (%) 2) Applying the Bitwise operator.

Modulus operator in Program 1

/* 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;
}

Program 2: Applying the 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;
}

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button