Advertisement
jdalbey

Recursive Factorial in Java

May 20th, 2014
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.32 KB | None | 0 0
  1. public class Factorial
  2. {
  3.     public static String fact(int n)
  4.     {
  5.         if(n == 1)
  6.         {
  7.             return "1";
  8.         }
  9.         return n + " * " + (fact(n-1));    
  10.     }
  11.     public static void main(String[] args)
  12.     {
  13.         // prints "5 * 4 * 3 * 2 * 1"
  14.         System.out.println(fact(5));
  15.     }
  16. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement