1
s in a Binary Representation1
s in the binary representation of an integer.
Examples:
Input 1: 5 (Binary 101) Output: 2 Input 2: 10 (Binary 1010) Output: 2
integer
to binary String
by using Integer's
built-in method and then count
1
s.
1. Declare and initialize a counter
variable to 0
inoreder to store the count of 1
s in the binary representation of an input.
2. Obtain the binary representation of an input with the help of Integer's
built-in method. i.e. Integer.toBinaryString(inputNum)
.
3. Traverse through each of the characters
of binary String
and increment the count
if the charater
is equal to '1'
.
4. Return count
.
public static int countNumberOfOnesInBinary(int inputNum) { }
C
Java
Python