用递归函数实现阶乘计算。

#include<iostream>
using namespace std;
long long factorial(int n){
    int fact;
    if(n==0||n==1)
        fact=1;
    else
        fact=n*factorial(n-1);
    return fact;
}

    int main(){
        int p;
        cout<<"Please enter an integer number(q to quit) :";
        while(cin>>p){
        long long fac=factorial(p);
        cout<<p<<"!="<<fac<<endl;
        cout<<"Please enter an integer number(q to quit) :";
        }
        return 0;
    }

猜你喜欢

转载自blog.csdn.net/weixin_44602460/article/details/86636942