Thread: Even/Odd number.

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    242

    Even/Odd number.

    How do I check whether the input is even number or whether it's an odd number?

    When I say the input I mean
    Code:
    char str[] = "12345";
    scanf("%s", str);
    Something like that.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You can either read it in as an integer, then check if the integer is even/odd, or you can read it in as a string then check if the last character is even/odd.

    For actually determining if a number is even/odd, there are a number of ways. The straightforward way is to check if n % 2 is 0, the other involves bitwise and, which is also straightforward if you think in terms of binary representation.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    I thought of the num % 2 thing.
    Look what I tried:
    Code:
    char str[] = "12345";
    printf("Enter a number:\n");
    ch = getchar();
    
    if(ch%2 == 0) oddNumber(ch);
    if(ch%2!=0) evenNumber(ch);
    Will it work with getchar? or should I:
    Code:
    ch = scanf("%s", str);
    Is that even possible?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Will it work with getchar?
    You say you tried. What did you find out?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    Hmm, I haven't actually compiled and run it, lol
    I just typed it on the same moment lol

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Hmm, I haven't actually compiled and run it, lol
    Then compile what you have and test if it works to your expectations.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with this compiler error
    By Evangeline in forum C Programming
    Replies: 7
    Last Post: 04-05-2008, 09:27 AM
  2. Even/Odd Number without % or /
    By oranges in forum C++ Programming
    Replies: 14
    Last Post: 03-14-2006, 10:29 AM
  3. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM
  4. parsing a number
    By juancardenas in forum C Programming
    Replies: 1
    Last Post: 02-19-2003, 01:10 PM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM