求教c++循环
#include<iostream>using namespace std;
int fact(int n)
{
int result=1;
if(n<0)
return -1;
else if(n==0)
return 1;
while (n>1)
{
result*=n;//这一步的循环转换具体流程是? result*什么然后得到n?难道是直接n*result(1)=n?
n--;
}
return result;
}
int main()
{
int n;
cout<<"please intput a number n to calculte n!:";
cin>>n;
cout<<n<<"!"<<fact(n)<<endl;
return 0;
}