Even or Odd number

Write a program to find a number if even or odd with out using conditional operator and if-else?

Showing Answers 1 - 18 of 18 Answers

anurag

  • Apr 9th, 2015
 

Using swich

Code
  1. scanf("%d",num);

  2. switch(num%2) {

  3.  

  4. case 0: printf("num is even");

  5.  

  6. case 1: printf("num is odd");

  7.  

  8. default: printf("wrong choice");

  9.  

  10. }

  Was this answer useful?  Yes

urvashi

  • Apr 10th, 2015
 

Code
  1. scanf("%d",num);

  2. num%2==0?printf("even"):printf("odd");

  Was this answer useful?  Yes

Baiju M P

  • Jul 10th, 2015
 

Code
  1. scanf("%d",num);

  2. num&1?printf("odd"):printf("even");

  Was this answer useful?  Yes

Alejandro Visedo

  • Mar 5th, 2016
 

Using only bit operators.

Code
  1. char* text[2] = { "even", "odd" };

  2. int num;

  3. scanf( "%d", &num);

  4. int index = num & 0x00000001;

  5. printf( "%d is %s

  6. ", num, text[index]);

  Was this answer useful?  Yes

debakanta rout

  • Apr 2nd, 2016
 

int num
scanf("%d",&nnum);
char *p[2]={"even","odd"};
printf("%d" is %s",num, p[num&1]);

Markandayan

  • Sep 11th, 2017
 

By using bitwise opertor
(even_number & 1) -> 0
(odd_number & 1) -> 1

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions