30 Day code challenge-python

Ganesh Uthiravasagam
2 min readMar 17, 2021

Day 10— Binary Numbers

Task

Given a base-10 integer, n, convert it to binary (base-2). Then find and print the base- integer denoting the maximum number of consecutive 1's in n’s binary representation. When working with different bases, it is common to show the base as a subscript.

Sample input and output

Input Format

A single integer, n.

Output Format

Print a single base-10 integer that denotes the maximum number of consecutive 1's in the binary representation of n.

Solution

Explanation

Line 1: Gets input from the user

Line 2: Converts the input to binary format (base-2)

Line 4, 5,6: Initially two variables are set to 0 and a for loop is generated

Line 10–14: If num is 1 then CURRENT variable gets incremented or else the maximum value between MAXIMUM variable and CURRENT variable is modified.

Line 16: Finally the maximum value between two variables is printed

Bonus Tip: max(MAXIMUM, CURRENT) eg: 11 binary value is 1011 which has 2 consecutive ones therefore, max(1, 2) = 2 gets displayed

See you on day 11

--

--