循环计算n的阶乘

版权声明:本文为@那年聪聪 原创文章,未经博主允许不得转载。 https://blog.csdn.net/duan19920101/article/details/55504857

循环计算n的阶乘

#include<iostream>
#include<ctime>

using namespace std;

double f(int x)
{
	if (x < 0)
		return 0;
	else if (x == 0)
		return 1;
	else
		return x*f(x - 1);

}

int main()
{
	//反复输入,计算阶乘
	int n;
	for (;;)
	{
		cout << "请输入一个正整数:";
		cin >> n;
		if (n == 0)
			break;
		if (n < 0)
		{
			cout << "不能是负数" << endl;
			continue;
		}
			
		cout << n << "的阶乘为:" << f(n) << endl;
	}
	return 0;
}


猜你喜欢

转载自blog.csdn.net/duan19920101/article/details/55504857
今日推荐