Saturday, 17 January 2015

Factorial program using recursion in java

 Java program for factorial using recursion

public class factorial {

    int fact(int n){
        if (n==1) {
            return 1;
        }
        else {
            return (n*fact(n-1));
        }       
    }
    public static void main(String[] args) {
        factorial obj=new factorial();
        System.out.println("fact="+obj.fact(4));
    }
   
}

No comments:

Post a Comment