# Python program to check if there are K consecutive 1’s in a binary number?

PythonProgrammingServer Side Programming

#### Beyond Basic Programming - Intermediate Python

Most Popular

36 Lectures 3 hours

#### Practical Machine Learning using Python

Best Seller

91 Lectures 23.5 hours

#### Practical Data Science using Python

22 Lectures 6 hours

First we take a user input string with the combination of 1’s and 0’s.then create a new string with 1’s,then check if there is any p number of consecutive 1’s is present or not. If present then display FOUND otherwise NOTFOUND.

## Example

Binary number ::1111001111
Enter consecutive 1’s :3
Consecutive 1's is Found


## Algorithm

Step 1: input a string with the combination of 1’s, it’s stored in the variable X and 0’s and p is the consecutive 1’s in a binary number.
Step 2: form a new string of p 1’s.
newstring=”1”*p
Step 3: check if there is p 1’s at any position.
If newstring in X
Display “FOUND”
Else
End if


## Example code

# To check if there is k consecutive 1's in a binary number
def binaryno_ones(n,p):
# form a new string of k 1's
newstr = "1"*p

# if there is k 1's at any position
if newstr in n:
print ("Consecutive 1's is Found")
else:

# driver code
n =input("Enter Binary number ::")
p = int(input("Enter consecutive 1's ::"))
binaryno_ones(n, p)


## Output

Enter Binary number ::1111001111
Enter consecutive 1's ::3
Consecutive 1's is Found

Updated on 30-Jul-2019 22:30:23