Recursion

Apr 13, 2011 at 7:03am
Hi freinds,
this is my code for simple recursion program to calculate factorial.
wheneve i return the different value for eg,
1.for return 1, the ans will be 720, which is correct one.
2.for return 0, the ans will be 0.
3.for return 2, the ans will be 1440.

#include<iostream>
using namespace std;


int factorial(int no)
{
if(no==1)
return 1;
//return 0;
//return 2;
else
return (no * factorial(no-1));
}

int main()
{
cout<<"Fatorial is "<<factorial(6);
}

plese help me out..
Apr 13, 2011 at 7:14am
Well your factorial function is working fine. It doesn't account for negative numbers or 0... but it's fine.

What's your problem? You probably can't go above 13, just because that's about the limit of a 32bit number.
Apr 14, 2011 at 4:51am
hi,

int factorial(int no)
{
if(no==1)
return 1;
//return 0;
//return 2;

when i remove comment from return 0 or return 2, the ans will be different..

for e.g

int factorial(int no)
{
if(no==1)
//return 1;
return 0;
//return 2;
Apr 14, 2011 at 8:29am
closed account (D80DSL3A)
when i remove comment from return 0 or return 2, the ans will be different..

That's right, so don't do that. As ultifinitus said, your function is correct. 1 = the correct return value.
Apr 14, 2011 at 9:17am
closed account (z05DSL3A)
1
2
3
4
5
6
7
8
9
int factorial(int no)
{
    if(no < 0) // Guard against negative numbers
        return 0;  // See edit.
    if(no <=1)  // 0! and 1! = 1
        return 1;
    else
        return (no * factorial(no-1));
}


Edit:
zero is returned to indicate an error! Appropriate error handling code should be used here.
Last edited on Apr 14, 2011 at 10:19am
Apr 14, 2011 at 9:55am
This is the right factoriel function:
1
2
3
4
5
int factorial(int n)
{
if(n==0) return 1;
return (n * factorial(n-1));
}

Last edited on Apr 14, 2011 at 9:56am
Apr 14, 2011 at 10:15am
closed account (z05DSL3A)
RisteMK,

factorial(-1) --> Boom!
Apr 14, 2011 at 10:24am
yes, i knew that. but when you call the function you will put ' if(n>=0) factorial(n); ' and else print 'put positive number or 0'. then the program will be ok..
Apr 14, 2011 at 11:45am
thanks guys...
but i want to know why answer is changed when i return different values. thats my question..
once again thanks for help..
Apr 14, 2011 at 12:49pm
closed account (z05DSL3A)
bally28 wrote:
this is my code for simple recursion program to calculate factorial.
bally28 wrote:
but i want to know why answer is changed when i return different values.
It it really your code or have you just copied it from somewhere and do not understand what is going on with it?
Apr 14, 2011 at 1:30pm
Grey Wolf
Hi,
thats my code and i know that how recursion works.
But i just want to know why last value returned is also get multiplied with final answer..
for e.g
return 2;

720*2 = 1440 ?
Apr 14, 2011 at 1:54pm
closed account (z05DSL3A)
The last value returned, as you put it, is actually the first value returned.

If you call factorial(2) it wants to return 2 * factorial(2 - 1), so it calls factorial(2 - 1). As this is 1! it has to return 1 (anything else is wrong*). If you do return 2 to the previous caller that would return 2*2 = 4.

3! = 3 * 2!
    = 3 * 2 * 1!
    = 3 * 2 * 1


* You could call factorial(0) but that would result in a pointless function call and multiplication operation as 0! is also 1.
Apr 15, 2011 at 5:43am
Hi, Grey Wolf
thanks for ans. My doubt is clear..
Thanks to all for their help..
Topic archived. No new replies allowed.