# Even or Odd (practice problem)

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 09-13-2010
Amphibian
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); }```
• 09-13-2010
CommonTater
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.
• 09-13-2010
Babkockdood
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.
• 09-13-2010
neandrake
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                */ }```
• 09-13-2010
Swarvy
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.
• 09-13-2010
neandrake
swarvy - you certainly can do something like that. in fact there are quite a few ways of determining even/odd.
• 09-13-2010
CommonTater
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.
• 09-14-2010
GReaper
Quote:

Originally Posted by CommonTater
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!!!
• 09-14-2010
EVOEx
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.
• 09-17-2010
Amphibian
Quote:

Originally Posted by CommonTater
[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.
• 09-17-2010
EVOEx
Quote:

Originally Posted by Amphibian
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?
• 09-20-2010
Amphibian
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.
• 09-20-2010
CommonTater
Quote:

Originally Posted by Amphibian
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.
• 09-21-2010
EVOEx
Quote:

Originally Posted by Amphibian
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?
• 09-22-2010
homer_3
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?
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last