c++计算n的阶乘(用循环和递归)

1.循环

//计算阶乘
#include  
using namespace std;  
int fct(int *p) {  
    int sum=1;
    while(*p>=2){
        sum*=((*p)--);
    }
    return sum;
}  
int main() {  
     cout<<"输入你想计算阶乘的n"<>n;
     cout<<"n的阶乘是:"<

2.递归

//计算阶乘
#include  
using namespace std;  
// 使用递归计算阶乘  
int fct(int n) {  
    if (n==1) return 1;  
    else return n*fct(n - 1);  
} 
int main() {  
     cout<<"输入你想计算阶乘的n"<>n;
     cout<<"n的阶乘是:"<

你可能感兴趣的:(c/c++,c++,算法,数据结构)