• 10-20-2001
cwd
How would I write a c program which will recursively compute the product of all even numbers greater than 0 and <= N, for values of N={16,7,9}, and print the results to an output file. For instance, if N=5, the product would be 4 x 2=8.

This recursive function in my program should accept a single value for N and for every call to that recursive function print out 1. how many times the recursive function has been called so far, 2. the value of the argument to the recursive function for this particular call, and 3. the value of the product so far(before the next recursive call). Main() is not the recurisve function. An example output for N=5 for what I want it to do might be:

Main: CALL ARGUMENT PRODUCT

RECU: 1 4 4
RECU: 2 2 8

MAIN: The product for N=5 is 8.

Here is what I got so far and had some help with other another programmer, but still stuck*NOTE(I know the program does not compute the right value, I need help on that too, plus I don't know what to do about passing the 3 values for N into the function)
code:

main()
{
int num=9;
recurse(num);

}

int recurse(int num)
{
if(num%2!=0)
num--;

int r=0;
if(num>0)
r=num*(num-2)+recurse(num-2);

printf("%d",r);
return r;

}
• 10-20-2001
Unregistered
easy:
just change this
int r=0; <<----- COMMENT THAT OUT, each time it calls the recurse function, it keeps resetting r to zero, thus you will never have the correct sum.
The rest is fine
if(num>0)
r=num*(num-2)+recurse(num-2);
• 10-21-2001
zen
No, you'll need r initialised to zero, otherwise calculating r=n*(n-2) will produce random results. You could create a couple of static variables to store the running total and function call number -

Code:

```#include <stdio.h> int recurse(int num) {         static int call;         static int total;         int r=0;         if(num%2!=0)         num--;                 if(num>0)         {                 call++;                 printf("Call: %d Arg: %d Running Total: %d\n",call,                                 num,total+=num*(num-2));                 r=num*(num-2)+recurse(num-2);         }           return r; } int main() {         int num=9;         printf("\nTotal: %d\n",recurse(num));         return 0; }```
• 10-21-2001
C-Learn
/*maybe buggy.!! but try this */

int product_of_evens(int i)
{
if (i<=0)
return 1;
if((i%2)==0)
return i*(product_of_evens(i-1));
else
return product_of_evens(i-1);
}
• 10-21-2001
C-Learn
/*maybe buggy.!! but try this */

int product_of_evens(int i)
{
if (i<=0)
return 1;
if((i%2)==0)
return i*(product_of_evens(i-1));
else
return product_of_evens(i-1);
}