Extra Long Factorials || JAVA || HackerRank
Problem No. 1
Max Score:
Difficulty: 
You are given an integer . Print the factorial of this number.
Input
Input consists of a single integer , where .
Input consists of a single integer , where .
Output
Print the factorial of .
Print the factorial of .
Example
For an input of , you would print .
For an input of , you would print .
Note: Factorials of  can't be stored even in a  long long variable. Big integers must be used for such calculations. Languages like Java, Python, Ruby etc. can handle big integers, but we need to write additional code in C/C++ to handle huge values.
We recommend solving this challenge using BigIntegers.
Solution in JAVA:
import java.io.*;
import java.util.*;
import java.math.*;
public class Solution {
    static void run(int a) {
        int i=1;
        BigInteger fact=BigInteger.valueOf(1);
        while(i<=a){
            fact=fact.multiply(BigInteger.valueOf(i));
            i++;
        }
        System.out.println(fact);
    }
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        run(n);
        in.close();
    }
}
No comments: