dsa

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

Factorial2.java (814B)


      1 package lab7;
      2 
      3 import java.util.Scanner;
      4 
      5 public class Factorial2 {
      6 
      7     Scanner in = new Scanner(System.in);
      8     public static void main(String[] args) {
      9         long n;
     10 
     11         Factorial2 factorial = new Factorial2();
     12 
     13         n = factorial.getInput();
     14         System.out.println(factorial.recursiveFactorial(n));
     15 
     16     }
     17 
     18     public long recursiveFactorial(long n) {
     19         long 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 long getInput() {
     31         long userIn;
     32         System.out.print("Enter number to calculate factorial: ");
     33         userIn = Long.parseLong(in.nextLine());
     34         return userIn;
     35     }
     36 
     37 
     38 }