dsa

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

Factorial.java (834B)


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