大家帮下忙,谢谢!
我是学C++的,C++和C很相似,不知道C++代码行不行。
C++代码如下:
using namespace std;
int main()
{
int n,ans=1;//因为n≤10,所以int型够用
cout<<"n=";
cin>>n;
if(n<0 || n>10)
cout<<"The input is out of range";
else
{
for(int i=1;i<=n;++i)
ans=i;//12……n
cout<<"The factorial of "<<n<<" is "<<ans;
}
return 0;
}
include <stdio.h>
int main()
{
long fac( int n );
int n, y;
printf( "The factorial of " );
scanf( "%d", &n );
y = fac( n );
printf( "%d is %ld\n", n, y );
return(0);
}
long fac( int n )
– 希望的石头OL 2年前{
long f;
if ( n < 0 )
printf( "The input is out of range" );
else if ( n > 10 )
printf( "The input is out of range" );
else if ( n == 0, n == 1 )
f = 1 ;
else f = fac( n - 1 ) * n;
return(f);
}
这样写好像也不正确
– 希望的石头OL 2年前