Solved

Posted on 2011-10-14

I can't understand this 2 little pieces of code, can someone explain why both codes return "720"?

I think all I need is someone to explain or comment this 2 codes so I can understand.

I think all I need is someone to explain or comment this 2 codes so I can understand.

```
-------- CODE NUMBER 1 RETURN 720 -----------
#include <stdio.h>
int main(){
int i, resultado = (13 == 13);
for( i = 0; i < 6; i++)
resultado = resultado * (i+1);
printf("El resultado es %d", resultado);
return 0;
}
-------- CODE NUMBER 2 RETURN 720 ALSO -----------
#include <stdio.h>
int calcula(int x){
if ( x == 0)
return 1;
else
return x * calcula(x-1);
}
int main(){
printf("El resultado es %d", calcula(6));
return 0;
}
```

15 Comments

int main(){

int i, resultado = (13 == 13); /* 13 is equal to 13 therefore returns 0 (true) */

for( i = 0; i < 6; i++)

resultado = resultado * (i+1); /* 0 times anything is 0 */

printf("El resultado es %i", resultado); /* %f is a double you need an int */ /* change the %d to %i */

return 0;

}

-------- CODE NUMBER 2 RETURN 720 ALSO -----------

#include <stdio.h>

int calcula(int x){ /* is 6 == 0 no return false 1 */

if ( x == 0)

return 1;

else

return x * calcula(x-1); /* 1 x ((1-1) =0) = 0 */

}

int main(){

printf("El resultado es %i", calcula(6));

return 0;

}

you should get 0 both times

Both calculate the factorial of 6, one iteratively and the other recursively.

The first is simple enough, all numbers 1 to 6 are multiplied.

You can think of the second like this: the factorial of any number, n, is equal to n * the factorial of n - 1. I.e., n! = n(n - 1)!

here.

how you get 6! = 720 I have no idea.

I think you may be right, now could you please explain how I understand the following sentences:

"13==13" can I say that if it is true=1?

"return 1" return value

"return 0" return 0 value? or does not return a value?

My advice would be to take your time, delete that code above, and look up some explanations on recursion, etc., on Google.

0 = true

1 = false

you can define true and false

#undef true

#define true 1

#undef false

#define false 0

as for return codes

DOS error codes are 0-255 and when tested using the 'errorlevel' syntax mean anything above or including the specified value, so the following matches 2 and above to the first goto, 1 to the second and 0 (success) to the final one!

IF errorlevel 2 goto CRS

IF errorlevel 1 goto DLR

IF errorlevel 0 goto STR

gcc and VC++ True is 1 and False is 0

Dev C++ has them reversed

a good discussion is located here

#include <stdio.h>

int main()

{

int i, resultado = (13 == 13); ==> (13 == 13) is a comparison operator. This returns 1 if it is equal and 0 otherwise. In this case, both sides are 13 and hence they are equal - So, it returns 1. So, resultado gets the value 1.

for( i = 0; i < 6; i++)

resultado = resultado * (i+1);

==> When the value of i=0 - resultado = 1 * (0+1) = 1

==> When the value of i=1 - resultado = 1 * (1+1) = 2

==> When the value of i=2 - resultado = 2 * (2+1) = 6

==> When the value of i=3 - resultado = 6 * (3+1) = 24

==> When the value of i=4 - resultado = 24 * (4+1) = 120

==> When the value of i=5 - resultado = 120 * (5+1) = 720

printf("El resultado es %d", resultado); ==> So, this prints 720

return 0;

}

-------- CODE NUMBER 2 RETURN 720 ALSO -----------

#include <stdio.h>

int calcula(int x) ==> main() calls this with the value of 6.

{

==> When x is 6, it returns 6 * calcula(5) -- call# 1

==> When x is 5, it returns 5 * calcula(4) -- call# 2

==> When x is 4, it returns 4 * calcula(3) -- call# 3

==> When x is 3, it returns 3 * calcula(2) -- call# 4

==> When x is 2, it returns 2 * calcula(1) -- call# 5

==> When x is 1, it returns 1 * calcula(0) -- call# 6

==> calcula(0) returns 1.

==> Now it returns 1 to the call where calcula() was called with 0.

That is call# 6 in the above steps = 1 * calcula(0) = 1*1 = 1

==> This returns to the call where calcula() was called with 1.

That is call# 5 in the above steps = 2 * calcula(1) = 2*1 = 2

==> This returns to the call where calcula() was called with 2.

That is call# 4 in the above steps = 3 * calcula(2) = 3*2 = 6

==> This returns to the call where calcula() was called with 3.

That is call# 3 in the above steps = 4 * calcula(3) = 4*6 = 24

==> This returns to the call where calcula() was called with 4.

That is call# 2 in the above steps = 5 * calcula(4) = 5*24 = 120

==> This returns to the call where calcula() was called with 5.

That is call# 1 in the above steps = 6 * calcula(5) = 6*120 = 720

if ( x == 0)

return 1;

else

return x * calcula(x-1);

}

int main()

{

printf("El resultado es %d", calcula(6)); ==> calcula(6) goes into a recursive call and returns 720, as explained above and gets printed.

return 0;

}`

In this call I dont understand why you received 24

That is call# 3 in the above steps = 4 * calcula(3) = 4*6 = 24

what I am doing (replacing) is :

return x * calcula(x-1); i am replacing with this formula x*x-1 is this correct?

2*2-1 = 2*2 =2 this is ok

3*3-1 = 3*2 =6 this is ok

4*4-1 = 4*3 =12 this should be 24, why?

> That is call# 3 in the above steps = 4 * calcula(3) = 4*6 = 24

calcula(3) is the value that we got in the previous step.

That is:

3 * calcula(2) = 3*2 = 6

In the code that you have posted, at line# 19 we have:

return x * calcula(x-1);

Now when you do: calcula(4)

it becomes:

4 * calcula(3)

And we know that calcula(3) is 6.

So, the value becomes

4 * 6 = 24.

Join the community of 500,000 technology professionals and ask your questions.

Connect with top rated Experts

**19** Experts available now in Live!