Even/Odd number.

This is a discussion on Even/Odd number. within the C Programming forums, part of the General Programming Boards category; How do I check whether the input is even number or whether it's an odd number? When I say the ...

  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
    20,980
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    20,980
    Will it work with getchar?
    You say you tried. What did you find out?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    20,980
    Hmm, I haven't actually compiled and run it, lol
    Then compile what you have and test if it works to your expectations.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 09: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, 12:10 PM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 04:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21