C program to check odd or even number - 7 easy ways

C program to check odd or even: We can check whether a given number is odd or even by using different methods with a code in c language. In decimal number system even numbers are divisible by 2 while odd are not. So we can use modulus operator(%) which returns remainder, For example 7%2 gives 1 ( remainder when an even number is divided by 2). Even numbers are of the form 2*p and odd are of the form (2*p+1) where p is an integer.

C program to check odd or even using modulus operator


#include <stdio.h>
 
int main()
{
   int n;
 
   printf("Enter an integern");
   scanf("%d", &n);
 
   if (n%2 == 0)
      printf("Evenn");
   else
      printf("Oddn");
 
   return 0;
}

Using bitwise AND (&) operator we can check odd or even, as an example consider binary of 3 (0011) when we perform 3 & 1 the result will be 1 and you may observe that the least significant bit of every odd number is 1, so ( odd_number & one ) will be one always and also ( even_number & one ) is zero.

C program to check odd or even using bitwise operator


#include <stdio.h>
 
int main()
{
   int n;
 
   printf("Enter an integern");
   scanf("%d", &n);
 
   if (n & 1 == 1)
      printf("Oddn");
   else
      printf("Evenn");
 
   return 0;
}

Find odd or even using conditional operator


#include <stdio.h>
 
int main()
{
   int n;
 
   printf("Input an integern");
   scanf("%d", &n);
 
   n%2 == 0 ? printf("Evenn") : printf("Oddn");
 
   return 0;
}

Division of integer by an integer always returns an Integer value if you are not using the type casting, For example the result of 3/2 will be 1. So we can take advantage of this and may use it to find whether the number is odd or even. Consider an integer n we can first divide by 2 and then multiply it by 2 if the result is the original number then the number is even otherwise the number is odd.

C program to check odd or even without using bitwise or modulus operator


#include <stdio.h>
 
int main()
{
   int n;
 
   printf("Enter an integern");
   scanf("%d", &n);
 
   if ((n/2)*2 == n)
      printf("Evenn");
   else
      printf("Oddn");
 
   return 0;
}

C program to check odd or even using loop


#include<stdio.h>

void main(){
      int n,flag=0,index;
      printf("Enter an Integer :");
      scanf("%d",&n);
      for(index=1;index < n ;index++){
	if(flag==1) flag=0;
		else flag=1;
     }
     if(flag==1) printf("Even Number");
	else printf("Odd Number");

}

Using bit fields we can declare variable with only one bit of storage. Using this technique we can find whether a number odd or even. Here in example we declared a variable 'number' with only one bit storage and we are assigning its value with the value of the inputted variable. The variable 'number' can only store the least significant bit so for all even numbers it will assign with 0 and all odd numbers it will assign with value 1.

C program to check odd or even using Bit Fields


#include <stdio.h>

struct integer{
	unsigned int number:1;
}struct_variable;
void main(){
   	int n;
    printf("Enter an Integer :");
    scanf("%d",&n);
    struct_variable.number=n;
    if(struct_variable.number==1) printf("Odd Number");
		else printf("Even Number");

}

We can check even or odd by using shift operators also may be it is not a better solution but trying to cover alternative. First we have to right shift the number by 1 and then left shift the result by 1. Check the result with the number and if both result and number is same means the number is event number otherwise its a odd number.

C program to check odd or even using Shift Operator


#include<stdio.h>

void main(){
  	int n;
    printf("Enter an Integer :");
    scanf("%d",&n);
    if((n >> 1) << 1 == n) printf("Even Number");
		else printf("Odd Number");

}