# Help with rounding INT's

• 05-19-2008
iAmFedor
Help with rounding INT's
Hi,

I have an exam comming up and i noticed something strange in one of the past papers, i wonder if anyone could please explain this to me.

The question is like so..........

a)The following is a recursive method coded in Java:
Code:

```int crank(int n){ if (n==1)                         return 0; else                 return 1 + crank(n/2); }```
iv)Trace the method call crank(12).

ii)n=12, n==1? No, 1+crank(12/2)=1+crank(6)
n=n=6, n==1? No, 1+[1 + crank(6/2)]= 1+[1+crank(3)]
n=3,n==1? No, 1+1+[1+crank(3/2)]=1+1+[1+crank(1)]
n=1, n==1? Yes, 1 + 1 + 1 + 0
= 3

i would have though 1.5 would be rounded to 2? is this the same in c++, 3/2 would return an int value of 1?

Thanks for any help.
• 05-19-2008
Daved
>> 3/2 would return an int value of 1?
Yes. In integer division (in both C++ and Java) the result is truncated, not rounded. In fact, any non-integer value when converted to an int is truncated, not rounded.
• 05-19-2008
iAmFedor
Quote:

Originally Posted by Daved
>> 3/2 would return an int value of 1?
Yes. In integer division (in both C++ and Java) the result is truncated, not rounded. In fact, any non-integer value when converted to an int is truncated, not rounded.

thanks!