Last active
December 6, 2017 04:43
-
-
Save jryebread/493ed5f386785a4a4db67c7df3224e63 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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