Skip to content

Instantly share code, notes, and snippets.

@jryebread
Last active December 6, 2017 04:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jryebread/493ed5f386785a4a4db67c7df3224e63 to your computer and use it in GitHub Desktop.
Save jryebread/493ed5f386785a4a4db67c7df3224e63 to your computer and use it in GitHub Desktop.
// Recursion - James Riback
package recursionones;
public class RecursionOnes {
static int getOnes(int num, int count) // number of times the value 1 appears in a number converted to binary
{
if(num >0)
{
count = num % 2 == 1 ? count + 1 : count; // if the remainder is equal to 1, we count it
return getOnes(num/2, count);
}
return count;
}
public static void main(String[] args) {
System.out.println("Number of ones: " + getOnes(11, 0));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment