Mail Archives: djgpp/1999/02/08/01:12:52
instead of
--passed
try
passed - 1
This works because parenthesis have higher precedence than *. so ur
--passed gets executed first before the multiplication. For eg.
passed = 5.
then
the return st --passed leads to passed = 4
raju
On Mon, 8 Feb 1999, Luis Pistoia wrote:
> Could anyone give me a little help?
> I'm trying to use recursive programming for getting a factorial of a number. The source I've made is:
>
> #include <stdio.h>
> #include <stdlib.h>
> long int rcs_factorial(long int);
>
> main(){
> int num;
> printf("Factorial of: ");
> scanf("%d",&num);
> printf(" %ld",rcs_factorial(num));
> }
>
> long int rcs_factorial(long int passed){
> if(passed <=1 ){
> printf("%d = ",passed);
> return 1;
> }
> else{
> printf("%d * ",passed);
> return passed * rcs_factorial(--passed);
> }
> }
>
> But it doesn't work. It does the rigth number of calls to rcs_factorial but fails at doing the first multiplication.
>
> Thanks.
> Luis.
>
>
- Raw text -