Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8367769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:14:30+00:00 2026-06-09T13:14:30+00:00

This is a challenging problem I came across while I was giving my internship

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T13:14:31+00:00Added an answer on June 9, 2026 at 1:14 pm

    Seth Carnegie’s answer can fail for certain inputs. In particular, on my system, it fails for an input of 2147483647, indicating that it’s even (at least on my system), because converting that value to float loses precision.

    Here’s an improved solution based on his:

    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    int main(int argc, char **argv) {
        const char *const even_or_odd[] = { "even", "odd" };
        for (int i = 1; i < argc; i ++) {
            const int n = atoi(argv[i]);
            printf("%d is %s\n",
                   n,
                   even_or_odd[(int)fmod((unsigned char)n, 2.0)]);
        }
        return 0;
    }
    

    The for (int i = ... syntax is “new” in C99; if your compiler doesn’t support it, declare int i; above the loop.

    The values to be tested are taken from the command line arguments. It would be easy enough to modify the program so they’re taken from stdin or elsewhere.

    The atoi() function does no error checking, so don’t expect meaningful results if you give it something that’s not a decimal integer.

    Converting the value of n to unsigned char before passing it to fmod() give a result with the same parity (odd-or-evenness) as n, but that won’t lose precision when converted to double (that conversion happens implicitly because fmod() takes double arguments). The standard-defined semantics of conversion to an unsigned type are such that this will work correctly even on systems that use a representation other than two’s-complement.

    It’s just barely possible that converting from unsigned char to double could lose precision. This would require unsigned char to have an implausibly large upper bound. double must have at least 10 decimal digits of precision, or about 33 or 34 bits; losing precision would require unsigned char to be at least 34 or so bits (it’s likely I have an off-by-one error or two in there). Such a system could be conforming, but I doubt that any such systems exist in the real world.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

<hyperbole> Whoever answers this question can claim credit for solving the world's most challenging
this is my first question in here, and I would like to ask if
NOTE: This is a challenging problem for anybody who likes logic problems, etc. Consider
I have a challenging problem where I would like to use reflection on my
I have a challenging question. My requirement is like i have a xml file
This problem is a challenging one. Our application allows users to post news on
This question is challenging to get more understanding on Image Processing using pure C.
This is a challenging one that got me stumped while I was coding today.
This question is directly related to this SO question I posed about 15 minutes
This question is kind of a follow up to this question I asked a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.