Basic recursive method - factorial

bob picture bob · May 10, 2010 · Viewed 31.5k times · Source

I am practicing recursion and I can't see why this method does not seem to work. Any ideas?

    public void fact()
    {
        fact(5);
    }

    public int fact(int n)
    {
        if(n == 1){
            return 1;
        }
        return n * (fact(n-1));
    }
}

Thanks

Answer

Anthony Forloney picture Anthony Forloney · May 10, 2010

Your code seems to work but you are not doing anything with the returned value, put method call fact or fact(5) inside of a System.out.println and see what you get.