Monday, 4 June 2012

Recursion in java

public class MyClass {
public static void main(String[] args) {
    System.out.println(fact(5));
   
}   

public static long fact(long n){
    if(n <= 1){
        return 1;
    }
    else{
        return n*fact(n-1);
    }
}

}

Output:
120

No comments:

Post a Comment

Note: only a member of this blog may post a comment.