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);
}``````
yellowSnow commented: incorrect signature for main (when will people learn?) +0

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 …

``````if(n==1)
return(1);``````

Also, this should be

``````if (n == 0)
return 1;``````

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

## All 9 Replies

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

``````if(n==1)
return(1);``````

Also, this should be

``````if (n == 0)
return 1;``````
``````/*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;

}``````
``````#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;

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

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

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

commented: I feel your pain... +36

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);
}
``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.