Even or Odd (practice problem)

This is a discussion on Even or Odd (practice problem) within the C Programming forums, part of the General Programming Boards category; This is not homework but it is supposed to be the third warm up before I actually begin the homework. ...

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    8

    Even or Odd (practice problem)

    This is not homework but it is supposed to be the third warm up before I actually begin the homework. I asked my lab instructor but he said to declare the function but i do not know what he is talking about. It stops when it hits the
    Code:
     return ( even( n - 1 ));	/*     n is odd if and only n-1 is even
    I have deleted it, but when i do that the comp says everything is odd

    Code:
     #include <stdio.h>
    
    
    int odd(int n)
    {
       if ( n == 1 )	/*     if n is 1 then it is certainly true that it is odd      */
          return ( 1 );
       else
          return ( even( n - 1 ));	/*     n is odd if and only n-1 is even                */
    }
    
    int even(int n)
    {
       if ( n == 0 )	/*     if n is 0 then it is certainly true that it is even    */
          return ( 1 );
       else
          return ( odd( n - 1) );	/*     n is even if and only n-1 is odd                */
    }
    
    main()
    {
       int N;
    
       printf("Please input a n integer.\n");
       scanf("%d", &N);
    
       if ( odd(N) )
            printf("The number %d is odd.\n", N);
       else
            printf("The number %d is even.\n", N);
    }
    Last edited by Amphibian; 09-13-2010 at 03:37 PM.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    int Odd(int n)
      { return ((n / 2) * 2 != n); }
    In integer math (no remainders) dividing an odd number by 2 always gives an even result...
    13/2 = 6.

    Multiply that by 2 and you've got 12, not 13...

    So if you divide by 2 then multiply by 2, if you get back the same number, it had to be even.

    By comparing the result with the original number, if they are not equal it was an odd number.

    The function will return 0 (false) for even numbers 1 (true) for odd.

    If you want to test for even numbers...

    Code:
    int Even(int n)
      { return ((n / 2) * 2 == n); }
    There, I did your homework for you.

  3. #3
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Code:
    foo() {
         bar();
    }
    
    bar() {
         foo();
    }
    This causes stack overflow. This is what I use to find if a number is even or odd.

    Code:
    int even(int x) {
         if (x % 2 == 0) return 0;
         else return 1;
    }
    This function returns 0 if the argument is an even number and 1 is it's an odd number.

  4. #4
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    i'm guessing the problem is that the function even() is called before it is defined.

    Code:
    
    int odd(int);
    int even(int);
    
    int odd(int n)
    {
       if ( n == 1 )	/*     if n is 1 then it is certainly true that it is odd      */
          return ( 1 );
       else
          return ( even( n - 1 ));	/*     n is odd if and only n-1 is even                */
    }
    
    int even(int n)
    {
       if ( n == 0 )	/*     if n is 0 then it is certainly true that it is even    */
          return ( 1 );
       else
          return ( odd( n - 1) );	/*     n is even if and only n-1 is odd                */
    }
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  5. #5
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195
    Am I missing something or couldn't you just do something like this:

    Code:
    int odd(int n)
    {
            return ( n%2 );
    }
    
    int even(int n)
    {
            return ( !odd(n) );
    }
    All you have to do is write one of the functions and the other one is just the NOT of the first, since if a number is even by definition it can't be odd and vica versa.

  6. #6
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    swarvy - you certainly can do something like that. in fact there are quite a few ways of determining even/odd.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    int odd(int n)
       { return (n & 1); }
    Tests bit 0 of the value. Bit 0 is 1 for odd numbers 0 for even... returns 1 if odd 0 if even.

  8. #8
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,624
    Quote Originally Posted by CommonTater View Post
    Code:
    int odd(int n)
       { return (n & 1); }
    Tests bit 0 of the value. Bit 0 is 1 for odd numbers 0 for even... returns 1 if odd 0 if even.
    Nice!!!
    Devoted my life to programming...

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    There have been better proposals yet, but you should try to fix the original example first.

    A hint on how to fix it: try to put 1 into "even" manually and see what happens:
    even(1) = odd(0) = even(-1)...

    Try to think of a solution for this - if you can't think of one, come back here and ask for more help.

  10. #10
    Registered User
    Join Date
    Aug 2010
    Posts
    8
    Quote Originally Posted by CommonTater View Post
    [code]
    i

    Code:
    int Even(int n)
      { return ((n / 2) * 2 == n); }
    There, I did your homework for you.
    its not my homework, it is a practice problem. We do not turn this in. I tried to think about what whats wrong for about four days (two to three hours a day) and could not figure this out. So thats why I posted. I am not a computer science major, i just want to learn a little bit of programming for the sake of understanding it. Im starting to think that taking the class might have been a mistake.
    Last edited by Amphibian; 09-17-2010 at 02:13 PM.

  11. #11
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by Amphibian View Post
    its not my homework, it is a practice problem. We do not turn this in. I tried to think about what whats wrong for about four days (two to three hours a day) and could not figure this out. So thats why I posted. I am not a computer science major, i just want to learn a little bit of programming for the sake of understanding it. Im starting to think that taking the class might have been a mistake.
    Did you even read my post?

  12. #12
    Registered User
    Join Date
    Aug 2010
    Posts
    8
    I read the posts. I didnt say that they were not helpful. I was just responding to a comment from CommonTater. I was just frustrated.

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Amphibian View Post
    I read the posts. I didnt say that they were not helpful. I was just responding to a comment from CommonTater. I was just frustrated.
    In all due respect...
    My "I did your homework" quip was nothing more than a little bit humor...
    If I'd thought I was actually doing your homework for you I would NOT have posted to this thread.

  14. #14
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by Amphibian View Post
    I read the posts. I didnt say that they were not helpful. I was just responding to a comment from CommonTater. I was just frustrated.
    I posted the source of your problem, and apparently you did nothing to try to solve your problem. Or did you manage to fix it after all?

  15. #15
    Registered User
    Join Date
    Nov 2008
    Posts
    119
    Your logic is wrong. If you passed in odd(2), 2!=1 so odd() would call even(2-1), 1!=0 so even would call odd(1-1), and next thing you know you are dealing with negative numbers, which your code doesn't handle.

    Is this warm up supposed to familiarize you with recursion?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 01:04 AM
  2. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  3. Words and lines count problem
    By emo in forum C Programming
    Replies: 1
    Last Post: 07-12-2005, 03:36 PM
  4. inStream practice problem not working?
    By correlcj in forum C++ Programming
    Replies: 2
    Last Post: 10-27-2002, 04:58 PM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM

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