The factorial of the integer , written , is defined as:
Calculate and print the factorial of a given integer.
For example, if , we calculate and get .
Function Description
Complete the extraLongFactorials function in the editor below. It should print the result and return.
extraLongFactorials has the following parameter(s):
- n: an integer
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.
Input Format
Input consists of a single integer
Constraints
Output Format
Print the factorial of .
Sample Input
Sample Output
Explanation
25! = 25 x 24 x 23 x ...........3 x 2 x 1
Solution:-
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
class FactorialDemo {
public static void extraLongFactorials(int n) {
BigInteger result = new BigInteger(""+n);
for (int i = 1; i<n ; i++)
{
result = result.multiply(BigInteger.valueOf(n-i));
}
System.out.println(result);
}
}
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(bufferedReader.readLine().trim());
FactorialDemo.extraLongFactorials(n);
bufferedReader.close();
}
}