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
andswitch-case
can be used.
So any ideas?
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.
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.
Silly questions call for silly answers.
printf("Yes, the number is even or odd\n");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With