need help with factorial using recursion.

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: May 2005
Posts: 82
Reputation: indianscorpion2 is an unknown quantity at this point 
Solved Threads: 1
indianscorpion2 indianscorpion2 is offline Offline
Junior Poster in Training

need help with factorial using recursion.

 
-1
  #1
Aug 6th, 2005
hi i am having a problem with this code.for every input i am getting the same answer 32.any help would be highly appreciated


  1. /*program to find the factorial of a given number*/
  2. #include<stdio.h>
  3. main()
  4. {
  5. int n,fact;
  6. int facto(int );
  7. printf("\nenter the number for which you want to find the factorial");
  8. scanf("%d",&n);
  9. fact=facto(n);
  10. printf("\nthe factorial of yhe number %d is %d",n,fact);
  11. }
  12. int facto(int n)
  13. {
  14. int k;
  15. if(n==1)
  16. return(1);
  17. else
  18. k=n*fact(n-1);
  19. return(k);
  20. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Re: need help with factorial using recursion.

 
0
  #2
Aug 6th, 2005
This won't compile:

k=n*fact(n-1);

should be

k=n*facto(n-1);

After making that change, seems to work fine..


enter the number for which you want to find the factorial
7
the factorial of yhe number 7 is 5040

(Be careful though factorials get large very quickly and you have a risk of blowing out the size of an integer, etc.)
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,052
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: need help with factorial using recursion.

 
0
  #3
Aug 6th, 2005
Originally Posted by indianscorpion2
  1. if(n==1)
  2. return(1);
Also, this should be
  1. if (n == 0)
  2. return 1;
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 4
Reputation: F1uT3 is an unknown quantity at this point 
Solved Threads: 0
F1uT3's Avatar
F1uT3 F1uT3 is offline Offline
Newbie Poster

Re: need help with factorial using recursion.

 
0
  #4
Aug 6th, 2005
/*program to find the factorial of a given number*/
#include<stdio.h>
main()
{
int fact(int );
/*-----------------------------------------------------*/
int n,fact;
printf("\nenter the number for which you want to find the factorial");
scanf("%d",&n);

fact=fact(n);

printf("\nthe factorial of yhe number %d is %d",n,fact);

}
/*-----------------------------------------------------*/
int fact(int n)
{
if(n==1)  return(1);

else  return n*fact(n-1);
}
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 3
Reputation: ##k.k## has a little shameless behaviour in the past 
Solved Threads: 0
##k.k## ##k.k## is offline Offline
Newbie Poster

factorial using recursion.

 
-1
  #5
Aug 1st, 2009
  1. #include<stdio.h>
  2. #include<conio.h>
  3. int factorial (int);
  4. void main()
  5. {
  6. auto int num , res;
  7. clrscr();
  8. printf("Enter the Number.....");
  9. scanf("%d",&num);
  10. res = factorial(num);
  11. printf("Factorial Of %d Is %d" , num , res);
  12. getch();
  13. }
  14. int factorial(int num)
  15. {
  16. int res =1;
  17. if(num!=0)
  18. res = num * factorial(num-1);
  19. return res;
  20.  
  21. }
Last edited by ##k.k##; Aug 1st, 2009 at 4:03 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 3
Reputation: ##k.k## has a little shameless behaviour in the past 
Solved Threads: 0
##k.k## ##k.k## is offline Offline
Newbie Poster

factorial using recursion.

 
-1
  #6
Aug 1st, 2009
  1. #include<stdio.h>
  2. #include<conio.h>
  3. int factorial (int);
  4. void main()
  5. {
  6. auto int num , res;
  7. clrscr();
  8. printf("Enter the Number.....");
  9. scanf("%d",&num);
  10. res = factorial(num);
  11. printf("Factorial Of %d Is %d" , num , res);
  12. getch();
  13. }
  14. int factorial(int num)
  15. {
  16. int res =1;
  17. if(num!=0)
  18. res = num * factorial(num-1);
  19. return res;
  20.  
  21. }
Last edited by ##k.k##; Aug 1st, 2009 at 4:03 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 201
Reputation: yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold 
Solved Threads: 35
yellowSnow's Avatar
yellowSnow yellowSnow is offline Offline
Posting Whiz in Training

Re: factorial using recursion.

 
0
  #7
Aug 1st, 2009
NO - void main is bad! At least use the signature int main() or int main(void) at the very least.
Manic twiddler of bits
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 201
Reputation: yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold 
Solved Threads: 35
yellowSnow's Avatar
yellowSnow yellowSnow is offline Offline
Posting Whiz in Training

Re: factorial using recursion.

 
1
  #8
Aug 1st, 2009
Damn! I didn't realise this thread was started 4 friggin' years ago.
Thanks a lot ##k.k## - why did you "revive" a thread from 4 years ago that had been answered?

- and void main is still bad!

Sheesh
Last edited by yellowSnow; Aug 1st, 2009 at 6:01 pm. Reason: pissed off
Manic twiddler of bits
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 3
Reputation: ##k.k## has a little shameless behaviour in the past 
Solved Threads: 0
##k.k## ##k.k## is offline Offline
Newbie Poster

Re: factorial using recursion.

 
0
  #9
Aug 3rd, 2009
Sorry Bro......I didn't see that the post is 4 year ago.........

I am just searching .......for this code.....i mean correct one...

but i didn't found the correct one.......

So i write it down......


I m bit confuse in 0!=1 or ZERO.....thats why....i am in search of this code....


By the way .....what is wrong with void main?????
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 1
Reputation: sanjeevtanwar is an unknown quantity at this point 
Solved Threads: 0
sanjeevtanwar sanjeevtanwar is offline Offline
Newbie Poster

modiffication in factorial problem by sanjeev

 
0
  #10
Oct 13th, 2009
/****************C Program for factorial by using recursion**************/
#include<stdio.h>
int fact(int a);
int facto(int s) //this function will calculate by recursive
{
if((s==0)||(s==1))
return 1;
else
return(s*facto(s-1));
}
main()
{ int n,result,result1;
printf("Pls enter ur num\n");
scanf("%d",&n);
result=facto(n);
result1=fact(n);

printf("facto of ur num= %d\n fact of ur num =%d\n",result,result1);
}


/***************************function for factorial without recursive*************/
int fact(int a)

{ int i,c=1;
for(i=1;i<=a;i++)
c=c*i;
return(c);
}
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC