Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +917541905230, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Thursday, July 16, 2020

C program to check even or odd using bitwise operator

C Program to check even or odd using bitwise operator
By Ahmad Irshad | C Programming Tutorial > Bitwise operator 

Write a C program to input any number and check whether the given number is even or odd using bitwise operator. Let's have a look at the below step by step logic to check whether the given number is even or odd.

Before writing any program you just imagine about output screen first. Like what input to be given, and output to be displayed. Let's have a look at the below example.

  1. INPUT

  2. Enter a number: 15

  OUTPUT

15 is odd number.

Required knowledge for this exercise

Logic to check whether the given number is even or odd using bitwise operator:

To check whether the given number is even or odd, we need to check LSB of a number. If LSB is 1 then the given number is odd and If LSB is 0 then the given number is even.

Now perform bitwise AND & to check LSB of a number. like if (num & 1) it retrns LSB of a number either 1 or 0. If LSB is 1 then the given number is odd otherwise even number.

Note: if (num & 1) is equivalent to if (num & 1 == 1) 

Check LSB of a number using bitwise operator

Program to check whether the given number is odd or even using bitwise operator: 

  1. /* C program to check whether the fiven number is odd or even */ #include <stdio.h> int main() { int num; /* Input number from user */ printf("Enter any number: "); scanf("%d", &num);
  2. /* if(num & 1) is equivalent to if(num & 1 == 1)
  3. * if it returns 1 the the number is odd else even
  4. */ if(num & 1) { printf("%d is odd number.", num); } else { printf("%d is even number.", num); } return 0; }

Output:

  1. Enter any number: 15
  2. 15 is odd number

As you can see in above image,15 & 1 returns to 1. Since, LSB of 15 is 1. since the given number 15  is odd number.

Above program can also be written using ternary (conditional) operator to minimize the code: 

  1. /* C program to check whether the fiven number is odd or even */ #include <stdio.h> int main() { int num; /* Input number from user */ printf("Enter any number: "); scanf("%d", &num);
  2. /* if(num & 1) is equivalent to if(num & 1 == 1)
  3. * if it returns 1 the the number is odd else even
  4. */ (num & 1) ? printf("%d is odd number.", num)
  5. : printf("%d is even number.", num); return 0; }

Visit to learn more about conditional or ternary operator.





Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Help others by sharing this page.

Ahmad Irshad

Author & Editor

I love blogging, teaching, learning computer science and sharing it to others. I've written and develped this site so that students may learn computer science related tutorials eaisly. MCA / MCITP

0 Comments:

Post a Comment

Please don't enter any spam link in the comment box.


Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec
Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec
Contact Us