Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tricky C Program to find even and odd [closed]

Tags:

This is a challenging problem I came across while I was giving my internship exam in Microsoft. The question goes like this:

User inputs a decimal number. The output should show whether the number is even or odd subject to constraint that only one printf, and no binary operator, logical operator, arithmetic operator, if-else and switch-case can be used.

So any ideas?

like image 846
sum2000 Avatar asked Dec 18 '11 06:12

sum2000


People also ask

How do you check if a value is even or odd in C?

In the program, the integer entered by the user is stored in the variable num . Then, whether num is perfectly divisible by 2 or not is checked using the modulus % operator. If the number is perfectly divisible by 2 , test expression number%2 == 0 evaluates to 1 (true). This means the number is even.

What is the best way to check if an integer is even or odd in C without using modulus?

Method 1: By using the bitwise (&) operator, a number can be checked if it is odd or even. Method 2: By multiplying and dividing the number by 2. Divide the number by 2 and multiply it by 2. If the result is the same as that of the input, then it is an even number otherwise, it is an odd number.


1 Answers

Silly questions call for silly answers.

printf("Yes, the number is even or odd\n"); 
like image 168
Keith Thompson Avatar answered Sep 28 '22 02:09

Keith Thompson