Created
June 1, 2019 16:37
-
-
Save vvviiimmm/8c6cd0592ab1281b700b4e427bfd3a6d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.InputStreamReader; | |
import java.math.BigInteger; | |
public class Main { | |
public static void main(String[] args) { | |
System.out.print("Please enter a number: "); | |
InputStreamReader read = new InputStreamReader(System.in); | |
BufferedReader in = new BufferedReader(read); | |
int number; | |
try { | |
number = Integer.parseInt(in.readLine()); | |
System.out.printf("Factorial of %d is %d", number, factorial(number)); | |
} catch (Exception e) { | |
System.out.print("Invalid number"); | |
} | |
} | |
public static BigInteger factorial(int n) { | |
BigInteger result = BigInteger.ONE; | |
for (int i = 1; i <= n; i++) { | |
result = result.multiply(BigInteger.valueOf(i)); | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment