Advertisement
NoSalt

Factorial.java

Jun 5th, 2012
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 0.59 KB | None | 0 0
  1. import java.util.Date;
  2.  
  3. public class Factorial {
  4.     private int computing = 0;
  5.    
  6.     public int factorial(int n) {
  7.         if (n == 1){
  8.             System.out.println(n + " = 1 [ " + new Date().getTime() + " ]");
  9.             return 1;
  10.         }
  11.         else{
  12.             computing = n * factorial(n - 1);
  13.             System.out.println(n + " * factorial(" + n + " - 1) = " + computing + " [ " + new Date().getTime() + " ]");
  14.             return computing;
  15.         }
  16.     }
  17.    
  18.     public static void main(String[] args) {
  19.         Factorial newFactorial = new Factorial();
  20.         int answer = newFactorial.factorial(6);
  21.         System.out.println("\nThe factorial of 6 is " + answer);
  22.     }
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement