delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/02/08/01:12:52

Date: Mon, 8 Feb 1999 11:12:06 +0530 (IST)
From: "Raju K.V" <rajukv AT wipinfo DOT soft DOT net>
X-Sender: rajukv AT tagore
To: djgpp AT delorie DOT com
Subject: Re: recursive factorial
In-Reply-To: <002301be5318$9d1b86e0$461b2bc8@luis>
Message-ID: <Pine.SV4.3.93.990208110633.29725A-100000@tagore>
MIME-Version: 1.0
Reply-To: djgpp AT delorie DOT com

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019