id/email
password
forgot password | create account
about | help | code help+videos | done | prefs
CodingBat code practice

 

oddHalfs


Given an integer value, keep dividing it in half until the result 1.

Count how many times the result of dividing the number is an odd number.
For example:

16 itself is not odd, so it isn't counted!
16 / 2 = 8, an even number, so we don't count it, but keep going...

8 / 2 = 4, an even number, so we don't count it, but keep going...

4 / 2 = 2, an even number, so we don't count it, but keep going...

2 / 2 = 1, although 1 is odd, we don't count it because this is our base case...

There were no odd values found along the way so we return 0!

But:

11  is odd, so it is counted!
11 / 2 = 5, an odd number, so we count another 1 and keep going...

5 / 2 = 2, and even number, so we don't count it, but keep going...

2 / 2 = 1, Even though 1 is odd, we don't count it because this is our base case...

There were two odd values found along the way so we return 2!

oddHalfs(30) → 3
oddHalfs(31) → 4
oddHalfs(32) → 0

...Save, Compile, Run (ctrl-enter)

public int oddHalfs(int num) { }

Editor font size %:
Shorter output


Forget It! -- delete my code for this problem

Progress graphs:
 Your progress graph for this problem
 Random user progress graph for this problem
 Random Epic Progress Graph

Java Help

Misc Code Practice

Difficulty: 200 Post-solution available

Copyright Nick Parlante 2017 - privacy