Logical error in finding the factorial by using the while loop

Hello,
Here is the program:
------------------------
#include <iostream>

using namespace std;

int main ()

{

int factorial;

cout<<"Enter a positive integer: ";

cin>>factorial;

for ( int x=factorial; x>1 ; x-- )

{

factorial = factorial * (factorial-1);
}

cout<<"The factorial is: "<<factorial<<endl;

return 0;

}
------------------------

What is wrong?
I got (as an example) 4!=17292 which clearly wrong. 4! should be 24.
Please what is wrong!
I got a headache!
Trace what your program does for a simple case, such as x = 3.

1
2
3
factorial = 3;  // input by user 
for( int x = 3; x > 1; x-- )
    factorial = factorial * ( factorial - 1 )


First time through the loop x = 3:
factorial = 3 * ( 3 - 1) = 6.

Second time through the loop x = 2:
factorial = 6 * ( 6 - 1 ) = 30.




Topic archived. No new replies allowed.