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
Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.
结论
如果你对教程有任何疑问,请在评论部分提出。
广告