943,782 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 25884
  • C RSS
Aug 6th, 2005
-1

need help with factorial using recursion.

Expand Post »
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. }
Similar Threads
Reputation Points: 9
Solved Threads: 1
Junior Poster in Training
indianscorpion2 is offline Offline
82 posts
since May 2005
Aug 6th, 2005
0

Re: need help with factorial using recursion.

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.)
Reputation Points: 68
Solved Threads: 18
Posting Pro in Training
winbatch is offline Offline
466 posts
since Feb 2005
Aug 6th, 2005
0

Re: need help with factorial using recursion.

Quote originally posted by indianscorpion2 ...
  1. if(n==1)
  2. return(1);
Also, this should be
  1. if (n == 0)
  2. return 1;
Team Colleague
Reputation Points: 1135
Solved Threads: 171
Super Senior Demiposter
Rashakil Fol is offline Offline
2,478 posts
since Jun 2005
Aug 6th, 2005
0

Re: need help with factorial using recursion.

/*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);
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
F1uT3 is offline Offline
4 posts
since Jul 2005
Aug 1st, 2009
-1

factorial using recursion.

  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.
Reputation Points: 3
Solved Threads: 0
Newbie Poster
##k.k## is offline Offline
6 posts
since Aug 2009
Aug 1st, 2009
-1

factorial using recursion.

  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.
Reputation Points: 3
Solved Threads: 0
Newbie Poster
##k.k## is offline Offline
6 posts
since Aug 2009
Aug 1st, 2009
0

Re: factorial using recursion.

NO - void main is bad! At least use the signature int main() or int main(void) at the very least.
Reputation Points: 651
Solved Threads: 35
Posting Whiz in Training
yellowSnow is offline Offline
201 posts
since Jul 2009
Aug 1st, 2009
1

Re: factorial using recursion.

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
Reputation Points: 651
Solved Threads: 35
Posting Whiz in Training
yellowSnow is offline Offline
201 posts
since Jul 2009
Aug 3rd, 2009
0

Re: factorial using recursion.

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?????
Reputation Points: 3
Solved Threads: 0
Newbie Poster
##k.k## is offline Offline
6 posts
since Aug 2009
Oct 13th, 2009
-1

modiffication in factorial problem by sanjeev

/****************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);
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sanjeevtanwar is offline Offline
1 posts
since Oct 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: How to do it?
Next Thread in C Forum Timeline: Int-datum Reader Program





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC