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);
}
Comments
incorrect signature for main (when will people learn?)

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

/*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);
}
#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;

}
Comments
use of void main (bad .. very very bad)
#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;

}
Comments
4 YEARS LATE just to repeat an answer already given, and make it worse in the process (void main - ewwwwww)

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

Comments
I feel your pain...

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

/****************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);
}

Edited 3 Years Ago by Dani: Formatting fixed

This article has been dead for over six months. Start a new discussion instead.