954,480 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

need help with factorial using recursion.

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

/*program to find the factorial of a given number*/
#include<stdio.h>
main()
{
int n,fact;
int facto(int );
printf("\nenter the number for which you want to find the factorial");
scanf("%d",&n);
fact=facto(n);
printf("\nthe factorial of yhe number %d is %d",n,fact);
}
int facto(int n)
{
int k;
if(n==1)
return(1);
else
k=n*fact(n-1);
return(k);
}
indianscorpion2
Junior Poster in Training
82 posts since May 2005
Reputation Points: 9
Solved Threads: 1
 

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.)

winbatch
Posting Pro in Training
466 posts since Feb 2005
Reputation Points: 68
Solved Threads: 18
 
if(n==1)
return(1);

Also, this should be

if (n == 0)
    return 1;
Rashakil Fol
Super Senior Demiposter
Team Colleague
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
 
/*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);
}
F1uT3
Newbie Poster
4 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 
#include<stdio.h>
#include<conio.h>
int factorial (int);
void main()
{
	auto int num , res;
	clrscr();
	printf("Enter the Number.....");
	scanf("%d",&num);
	res = factorial(num);
	printf("Factorial Of %d Is %d" , num , res);
	getch();
}
int factorial(int num)
{
	int res =1;
	if(num!=0)
	res = num * factorial(num-1);
	return res;

}
##k.k##
Newbie Poster
6 posts since Aug 2009
Reputation Points: 3
Solved Threads: 0
 
#include<stdio.h>
#include<conio.h>
int factorial (int);
void main()
{
	auto int num , res;
	clrscr();
	printf("Enter the Number.....");
	scanf("%d",&num);
	res = factorial(num);
	printf("Factorial Of %d Is %d" , num , res);
	getch();
}
int factorial(int num)
{
	int res =1;
	if(num!=0)
	res = num * factorial(num-1);
	return res;

}
##k.k##
Newbie Poster
6 posts since Aug 2009
Reputation Points: 3
Solved Threads: 0
 

NO - void main is bad! At least use the signature int main() or int main(void) at the very least.

yellowSnow
Posting Whiz in Training
203 posts since Jul 2009
Reputation Points: 651
Solved Threads: 35
 

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

yellowSnow
Posting Whiz in Training
203 posts since Jul 2009
Reputation Points: 651
Solved Threads: 35
 

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?????

##k.k##
Newbie Poster
6 posts since Aug 2009
Reputation Points: 3
Solved Threads: 0
 

/****************C Program for factorial by using recursion**************/
#include
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);
}

sanjeevtanwar
Newbie Poster
1 post since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You