class

i dont know whats wrong in my code..
its factorial
i dont know why the one or last number is not multiplying

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <string>
class Factorial
{

public:
    int computation();
    void setNumber(int );
private:
 int nFactorial;
};
int Factorial::computation()
{

for(int i=1,j=nFactorial; j-i>1; ++i  )
{
j-=i;
nFactorial*=j;
}
return nFactorial;
}
void Factorial::setNumber(int set_number)
{
nFactorial=set_number;
}
int main()
{
using namespace std;
int nNumber;
    Factorial newFactorial;
    cout<<"Enter a number: "<<endl;
    cin>>nNumber;
    newFactorial.setNumber(nNumber);
    cout<<nNumber<<"! = "<< newFactorial.computation();

}
/*
4!= n(n-1)(n-2)(n-3)
if n-3 is equals to 1
then stop the computation*/
Last edited on
replace line 15 to 21 with

1
2
3
for(int j=nFactorial; j > 1;)
    nFactorial*=--j;
return nFactorial;
k thanks
nFactorial*=--j;
I might split this line up, just to make sure it's doing what you think it's doing. Operator precedence might sort it out, but still...
Topic archived. No new replies allowed.