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;
}

4 comments:

Unknown 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

WUGI said...

Tired of watching TV? Go to the BGAOC website and earn with us best slot machine games It’s hard to lose and make money with us. Come on BGAOC and get your winnings.

Rekha said...

Thanks for the efforts in writing the wonderful article.
php interview questions and answers
salesforce interview questions
blue prism interview questions
javascript interview questions
ethical hacking interview questions
networking interview questions and answers

Magesh said...

Most helpful blog! It is the greatest comprehensive updates about this topic and Thank you for your sharing with us..!
Automation Anywhere Training in Chennai
Automation Anywhere Online Training
VMware Training in Chennai
VMware Course in Bangalore
Automation Anyware Online Course