java大数类阶乘
It is not possible to store factorial for large number like 50 into inbuilt data types like integer or long. Because factorial of 50 has almost 60 digits. Imagine how we can store it in int or long. We can find factorial of such numbers using BigInteger class defined in java.math package.
无法将大量(例如50)的阶乘存储到内置数据类型(例如整数或长整数)中。 因为50的阶乘几乎有60位数字。 想象一下如何将其存储为int或long类型。 我们可以使用java.math包中定义的BigInteger类找到此类数字的阶乘。
Below program shows how you can do this.
下面的程序显示了如何执行此操作。
Also Read: Factorial of Large Number in C and C++
另请阅读: C和C ++中的大数阶乘
Java大数分解程序 (Program for Factorial of Large Number in Java)
import java.math.BigInteger;
import java.util.Scanner;
class BigFactorial {
public static void main(String arg[]){
BigInteger fac=new BigInteger("1");
int n;
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number:");
n=sc.nextInt();
for(int i=2;i<=n;++i){
fac=fac.multiply(BigInteger.valueOf(i));
}
System.out.println(fac);
}
}
Output
输出量
Comment below if you are facing any difficulty to understand this program.
如果您在理解此程序时遇到任何困难,请在下面发表评论。
翻译自: https://www.thecrazyprogrammer.com/2016/04/factorial-large-number-java.html
java大数类阶乘