fahimkamal63

Factorial

Feb 25th, 2020
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.69 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.math.BigInteger;
  3. public class Factorial {
  4.     // Returns Factorial of N
  5.     static BigInteger factorial(int N)
  6.     {
  7.         // Initialize result
  8.         BigInteger f = new BigInteger("1"); // Or BigInteger.ONE
  9.  
  10.         // Multiply f with 2, 3, ...N
  11.         for (int i = 2; i <= N; i++)
  12.             f = f.multiply(BigInteger.valueOf(i));
  13.  
  14.         return f;
  15.     }
  16.  
  17.     // Driver method
  18.     public static void main(String args[]) throws Exception
  19.     {
  20.         Scanner input = new Scanner(System.in);
  21.         System.out.print("Enter number : ");
  22.         int n = input.nextInt();
  23.         System.out.println(factorial(n));
  24.     }
  25. }
Add Comment
Please, Sign In to add comment