神刀安全网

Factorial of Large Number in 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. 

Below program shows how you can do this.

Also Read: Factorial of Large Number in C and C++

Program for Factorial of Large Number in Java

import java.math.BigInteger; import java.util.Scanner;   class BigFactorial {     public static void main(String arg[]){         BigIntegerfac=new BigInteger("1");         int n;                  Scannersc=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

Factorial of Large Number in Java

Comment below if you are facing any difficulty to understand this program.

转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » Factorial of Large Number in Java

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
分享按钮