Wednesday, June 6, 2012

[Java]Factorials

public int factorial(int n) {
        if (n == 0) return 1;
        else return (n * factorial(n-1));     // tail recursion
    }

public int factorial(int n) {
        int product = 1;
        int i;
        for (i = n; i >= 1; i--)    
            product = i * product;            // without tail recursion
        return product;
}

1 comment:

Chandrika Madapudi said...

Hi, This is Chandrika from Chennai. I have read your blog and I got some knowledgeable information through this blog. Really useful blog. Keep update your blog.

Regards...
Java Training in Chennai