计算最大数字的阶乘

Calculate factorial of the largest numbers

本文关键字:阶乘 数字 计算      更新时间:2023-10-16

我想计算大于 30 的数字的阶乘,当我想计算阶乘时结果为 0 。

#include <iostream>
using namespace std;
void fun1();
int main()
{
fun1();
}
void fun1(){
int x  ; 
int  sum = 1 ;
cout << "Enter your number:";
cin >> x ;
if ( x >= 1 ){
for ( int i = x ; i > 1 ; i--){
sum  *=   i ;
}
} 
cout << "Factorial of " << x << " is :" << sum ;
}

您可以使用更大的数据类型(如果可用,unsigned long long int(。

您可以通过检查表达式中何时有 2 和 5 因素来欺骗一些数字,删除它们并记住在末尾输出额外的 0:

6! = 6 * 5 * 4 * 3 * 2 = ( 6 * 4 * 3 ) * (5*2) = "72""0"

最重要的是,您使用"大数"或"任意精度"库