Recursive Function in Java


☞A function designed in such a way that it calls itself in its body is called a recursive function.

Example

class Recursive{
    
    int fact(int n){
        if(n==0)
            return 1;
        else
            return(n*fact(n-1));
    }
    
    public static void main(String args[]){
        Recursive obj=new Recursive();
        int k=5,f;
        f=obj.fact(k);
        System.out.println(f);
    }

}

Output

120