Advertisement
brilliant_moves

BigFactorial.java

Aug 9th, 2012
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 0.70 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.math.BigInteger;
  3.  
  4. public class BigFactorial {
  5.  
  6.     /** Program:    BigFactorial.java
  7.     *   Purpose:    Find n! up to 10000! using Big Integers
  8.     *   Creator:    Chris Clarke
  9.     *   Created:    16.07.2012
  10.     */
  11.  
  12.     public static void main(String[] args) {
  13.         Scanner keyboard = new Scanner(System.in);
  14.         System.out.print( "Enter a number (max 10000): ");
  15.         int n = keyboard.nextInt();
  16.         BigInteger bigInt = new BigInteger (""+n);
  17.         System.out.println( "The factorial of "+n+" = "+n+"! = "+factorial( bigInt));
  18.     }
  19.  
  20.     public static BigInteger factorial( BigInteger b) {
  21.         return b.equals(BigInteger.ONE)?
  22.          BigInteger.ONE:
  23.          b.multiply( factorial( b.subtract( BigInteger.ONE)));
  24.     }
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement