# Thread: [Newbie] Factorial

1. ## [Newbie] Factorial

Hi, I'm doing my homework and I'm stuck on this factorial part. I know this is the code for n! from my text:

Code:
```#include <stdio.h>
double  mfact(int);
main()
{
int m;
printf("Enter an integer m>0: ");
scanf("%d",&m);
printf("m! is: %g\n",mfact(m));
}
double mfact(int m)
{
if(m==1)return 1;
else return m*mfact(m-1);
}```
But I need 2n! instead so I tried to modify the code to this:

Code:
```#include <stdio.h>
double  mfact(int);
main()
{
int m;
printf("Enter an integer m>0: ");
scanf("%d",&m);
printf("m! is: %g\n",mfact(m));
}
double mfact(int m)
{
if(m==1)return 2;
else return 2m*mfact(2m-1);
}```
Because I figured that 2n! = 2n*(2n-1) ... etc. so it would work but it's messed up. So in fact, I realized I don't get what this whole code is about. What is the mfact specifically?

2. I note that (2n)! = m!, where m = 2n.

I realized I don't get what this whole code is about. What is the mfact specifically?
mfact is a recursive function.

3. Thanks for the help. I don't know why but I'm still getting the wrong answer because when I entered in 2, I get 48 when I'm supposed to get 24.

Code:
```#include <stdio.h>
double  mfact(int);
main()
{
int m, n;
printf("Enter an integer m>0: ");
scanf("&#37;d",&n);
m = 2*n;
printf("2n! is: %g\n",mfact(m));
}
double mfact(int m)
{
if(m==2)return 2;
else return m*mfact(m-1);
}```

4. Aside from the fact that mfact(1) no longer works, and that your code formatting could be improved, it looks okay to me. You do not actually need another variable in main() though, since n can be reused.

5. Thanks! I got it now!

Edit: Just a quick question but is it possible to put m = 2n; like this:

Code:
```double mfact(int m)
{
m = 2n;
if(m==2)return 2;
else return m*mfact(m-1);
}```

6. You should just write mfact() to compute the factorial of any integer >= 0. Then just plug in 2*n when you call it. You're not going to accomplish or simplify anything by trying to change the body of the function itself. BTW, if you replaced "if (m == 1)" with "if (m == 0)" you could have it handle the case 0! == 1.

7. Because I figured that 2n! = 2n*(2n-1) ... etc. so it would work but it's messed up.
Typically, if that's truly what was meant, it would be written (2n)!. If that is the case,do what robatino said.

But to me it'd be more likely to ask something like finding 2*(n!).

8. Originally Posted by lazyturtle
Thanks! I got it now!

Edit: Just a quick question but is it possible to put m = 2n; like this:

Code:
```double mfact(int m)
{
m = 2n;
if(m==2)return 2;
else return m*mfact(m-1);
}```
Where did n come from?

if you mean m = 2m... m *= 2;

Popular pages Recent additions