C++入门经典-例4.5-利用循环求n的阶乘

1:代码如下:

// 4.5.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
using namespace std;
typedef unsigned int UINT;         //自定义类型
long Fac(const UINT n)            //定义函数
{
    long ret = 1;                //定义结果变量
    for(int i=1; i<=n; i++)            //累计乘积
    {
        ret *= i;
    }
    return ret;                    //返回结果
}

void main()
{
    int n ;
    long f;
    cout << "please input a number" << endl;
      cin >> n ;
    f = Fac(n);
    cout << "Result :" << f << endl;
}
View Code

运行结果:

原文地址:https://www.cnblogs.com/lovemi93/p/7519577.html