plzz correct my program!!

Hey everyone, i'm actually new in c++ and i'm trying to write a program that calculate fatorial, but it doesn't work for me i don't know what is the mistake that i made, well here's my program i appreciate if anyone of you can tell me thank you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include<stdio.h>
#include<stdlib.h>
#include<math.h>

int facto(int,int);
int n,p,FN,FP,FNP,CNP;
int main()
{
 n=5;
 p=2;
 facto(n,FN);
 facto(p,FP);
 facto(n-p,FNP);
 CNP=FN/(FP*FNP);
 printf("%d",CNP);

}
int facto(int x, int Fx);
int i;
{   
    Fx=1;
    for(i; >0; <=x)
    {
        Fx=Fx*i;
    }
}
'.h' libraries are deprecated. You should use cstdio, cstdlib]and [code]cmath (though I would personally use iostream and then output with std::cout instead of printf.

As far as your problem firstly I would like to mention it shouldn't compile. You need to remove the semi-colon on line 18 compiler errors should have mentioned that. Also line 19 and 20 are swapped and probably don't get a compiler for that since it would just put i in the global space but you don't need it to be global this variable is not necessary though. Though your for loop is really weird? I have no idea what you were trying to do with for(i; >0; <=x) anyways you might find a while loop easier.
1
2
3
4
5
while( x > 0)
{
    Fx *= x;
    --x; //or x-- on the line above instead of this line
}
or if you insist on a for loop something like:
1
2
3
4
for(x; x > 0; --x)
{
    Fx *= x;
}
or even
1
2
3
4
for(; x > 0; --x)
{
    Fx *= x;
}
aaah thank you soo much, it helped.
Topic archived. No new replies allowed.