1. ## Recursion stuck

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=9 for what I want it to do might be:

Main: CALL ARGUMENT PRODUCT

RECU: 1 8 8
RECU: 2 6 48
RECU: 3 4 192
RECU: 4 2 384

MAIN: The product for N=5 is 8.

Here is what I got so far with the help of others but I still can't figure out how to printout how many times the function has been called, the value of the argument to the function for this call, and value of the product so far(BEFORE THE NEXT FUNCTION CALL). I can get it to just print ouf the product after the whole program is done, for instance N=9, the product of all it's even numbers is 8*6*4*2=384
Notice for the example call it prints out call 1, argument 8, and product 8(product 8 because the first number it gets is 8 and so there is no other numbers yet to multiply it to for the total)
Last I still can't also figure out how to get is to run through and find the product of all even numbers of each number 16,7,9. Maybe I need to use an array and I also need to clear the screen for each new number, like after 16, it clears it for the next value 7.
code:

#include <stdio.h>

int product_of_evens(int num)
{
static int call;
call++;

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

}

int main()
{
int num=9;
printf("\nTotal: %d\n",product_of_evens(num));
return 0;
}

2. OK, here's some code that will do the first bit (I'll leave the multiple calls for the different numbers up to you - you can use a loop) -

Code:
``` 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,call==1?total=num:total*=num);
r=num*recurse(num-2);
}

if(num==0)
return 1;
else
return r;

}

int main()
{
int num=9;
printf("\nTotal: %d\n",recurse(num));
return 0;
}```

3. int product_of_evens(int i)
{
static int n=0,p=1;
if (i<=0)
return 1;
if((i%2)==0)
{
printf("%d %d %d\n",++n,i,p*=i);
return i*(product_of_evens(i-1));
}

else
return (product_of_evens(i-1));

}
/* the above programme actually performs 'i' number of iterations ..try one of these for exact iterations*/

int product_of_evens(int i)
{
static int n=0,p=1;
if (i<2)
return 1;
if((i%2)==0)
{
printf("%d %d %d\n",++n,i,p*=i);
return i*(product_of_evens(i-1));
}

else
{
printf("%d %d %d\n",++n,i,p*=--i);
return i*(product_of_evens(i-1));
}

}

/*------------------------------------------------------------*/

int product_of_evens(int i)
{
static int n=0,p=1;
if (i<=0)
return 1;
if((i%2)==0)
{
printf("%d %d %d\n",++n,i,p*=i);
return i*(product_of_evens(i-2));
}

else
{
printf("%d %d %d\n",++n,i,p*=--i);
return i*(product_of_evens(i-2));
}

}