C++ Primer(第5版) 练习 6.4
练习 6.4 编写一个与用户交互的函数,要求用户输入一个数字,计算生成该数字的阶乘。在main函数中调用该函数。
环境:Linux Ubuntu(云服务器)
工具:vim
代码块
/*************************************************************************
	> File Name: ex6.4.cpp
	> Author: 
	> Mail: 
	> Created Time: Mon 12 Feb 2024 10:28:33 PM CST
 ************************************************************************/
#include<iostream>
using namespace std;
int fact(int num){
    int result = 1;
    int n = num;
    while(n != 0){
        result *= n--;
    }
    return result;
}
int main(){
    int num;
    
    cout<<"Enter number: ";
    cin>>num;
    cout<<num<<"! = "<<fact(num)<<endl;
    return 0;
}
运行结果显示如下




















