# How to calculate factorial of large number in Java?

##
__Problem Description__ :

Write a program in Java that calculates factorial of a large number.

##
__Concept__ :

The factorial of non-negative integer is denoted by

**n!**and it is the product of all positive integers less than or equal to n. For example - 5! = 5 X 4 X 3 X 2 X 1 = 120.
For calculating factorial, we are using

**BigInteger**class that is present in**java.math.***package.##
__Java Program__ :

package com.javamultiplex.number; import java.math.BigInteger; import java.util.Scanner; /** * * @author Rohit Agarwal * @category Number Problems * @problem How to calculate factorial of large number? * */ public class FactorialOfLargeNumber { public static void main(String[] args) { Scanner input = null; try { input = new Scanner(System.in); System.out.println("Enter number : "); int num = input.nextInt(); BigInteger result = getFactorial(num); System.out.println("Factorial is : " + result); } finally { if (input != null) { input.close(); } } } private static BigInteger getFactorial(int num) { // Converting int to String. String number = String.valueOf(num); // Converting String to BigInteger. BigInteger n = new BigInteger(number); // ZERO=0 BigInteger ZERO = BigInteger.ZERO; // ONE=1 BigInteger ONE = BigInteger.ONE; // fact=1 BigInteger fact = BigInteger.ONE; // while(n!=0) while (!(n.equals(ZERO))) { // fact=fact*n fact = fact.multiply(n); // n=n-1 n = n.subtract(ONE); } return fact; } }

##
__Output__ :

##
__References__ :

Thank you friends, I hope you have clearly understood the solution of this problem. If you have any doubt, suggestion or query please feel free to comment below. You can also discuss this solution in our forum.

**Tags : Calculate factorial in Java, Solution in Java, large number, BigInteger, if else statement, while loop, String.**

How to calculate factorial of large number in Java?
Reviewed by Rohit Agarwal
on
12/28/2016
Rating:

## No comments:

Please provide your valuable comments. If you have any suggestion please share with me I will work on it and if you have any question or doubt please ask, don't hesitate. I am your friend, i will clarify all your doubts.