Hello, i'm a newbie with c++.. all help and explanation are welcome..my problem is, i try to make a factorial program in c++ . Here is my code:
#include <iostream>
using namespace std;
int main()
{
int a,b,fact=1;
char key;
do
{
cout<<"\nPlease enter a positive integer : ";
cin>>b;
for (a=b; a>=1; a--)
fact=fact*a;
cout<<"The output of "<<b<<"! is : "<<fact<<endl;
cout<<"\nPlease enter y to continue : ";
cin>>key;
}
while ( key == 'y' || key == 'Y');
system("pause");
return 0;
}
to make a single factorial calculation is easy for me. but i want to make it that just put any number and it will give the answer every time the program is repeated . For example, when i run my code in c++ it wil give me this:
Please enter a positive integer : 5
The output of 5! is : 120
Please press y to continue :
Now, when i enter y, it will goes like this:
Please enter a positive integer : 5
The output of 5! is : 120
Please press y to continue : y
Please enter a positive integer :
So, i decided to put 6, but the problem is 6! suppose to be 720.. but this program gave me 6! is 86400????
the ouput is like this:
Please enter a positive integer : 5
The output of 5! is : 120
Please press y to continue : y
Please enter a positive integer : 6
The ouput of 6! is : 86400
Please press y to continue :
It goes like that whenever i enter new value. It's like it multiply the first factorial value with the variable a;.. need your opinion and please don't give me direct answer..i need to learn!!