dsa

Data Structures & Algorithms - Spring 2018
Log | Files | Refs | README

Factorial3.java (946B)


      1 package lab7;
      2 
      3 import java.math.BigInteger;
      4 import java.util.Scanner;
      5 
      6 public class Factorial3 {
      7     Scanner in = new Scanner(System.in);
      8     public static void main(String[] args) {
      9         BigInteger n;
     10 
     11         Factorial3 factorial = new Factorial3();
     12 
     13         n = factorial.getInput();
     14         System.out.println(factorial.recursiveFactorial(n));
     15 
     16     }
     17 
     18     public BigInteger recursiveFactorial(BigInteger n) {
     19         BigInteger result;
     20         // double[] array = new double[1000000000];
     21         if (n.intValue() == 1 || n.intValue() == 0) {
     22             result = BigInteger.valueOf(1);
     23         }
     24         else {
     25             result = n.multiply(  recursiveFactorial(n.subtract(BigInteger.valueOf(1))));
     26         }
     27         return result;
     28     }
     29 
     30     public BigInteger getInput() {
     31         BigInteger userIn;
     32         System.out.print("Enter number to calculate factorial: ");
     33         userIn = in.nextBigInteger();
     34         return userIn;
     35     }
     36 }