C ++中数字阶乘的第一个数字
在本教程中,我们将编写一个程序来查找阶乘的第一位。让我们来看一个例子。
输入-7
输出-5
让我们看看解决问题的步骤。
初始化号码
查找数字的阶乘。
除以数字直到变成一位数字。
示例
让我们看一下代码。
#include <bits/stdc++.h>
using namespace std;
void findFirstDigitOfFactorial(int n) {
long long int fact = 1;
for (int i = 2; i <= n; i++) {
fact = fact * i;
}
while (fact >= 10) {
fact = fact / 10;
}
cout << fact << endl;
}
int main() {
int n = 7;
findFirstDigitOfFactorial(n);
return 0;
}输出结果如果执行上述程序,则将得到以下结果。
5