Even Odd without Mod Operator

This is a simple  Program in C++ for finding if a given number is Even or Odd without Mod Operator. This is a part of Mumbai University MCA Colleges. This is a Interview Question and Program

Even Odd with out Mod operator


#include <stdio.h>
#include <string.h>
int main()
{
int a=8;
( a & 1 == 1)? printf(" odd number\n") : printf(" even number \n");
return 0;
}


In this program, I have used the bitwise AND operator for checking the last bit of the number. In binary form, if the number has the last bit set to 1 then its a Odd number and if the last bit is set to 0 then its a even number.
Using a simple AND Operation, we find the last bit and compare if its 1. 
Using conditional operator the code is reduced to single line.



Hope this Program is useful to you in some sense or other. Keep on following this blog for more Mumbai University MCA College Programs. Happy Programming and Studying.

No comments:

Post a Comment