Program to check whether the given number has consecutive 0’s in the given base or not using Python

Introduction

Given a number N, the task is to find whether there is consecutive 0’s on converting it to base B.

Program to check whether the given number has consecutive 0’s in the given base or not using Python

Program

def find(ip_num, ip_base):
    base = convert(ip_num, ip_base)
    if (verify(base)):
        print("Consecutive 0 exists!")
    else:
        print("Consecutive 0 does not exists!")


def convert(ip_num, ip_base):


    x = 1
    y = 0
    while (ip_num != 0):
# Convert ip_num into given base
        temp = ip_num % ip_base
        ip_num = ip_num//ip_base
        y = temp * x + y
        x *= 10
    return y


def verify(ip_num):


    temp = False
    while (ip_num != 0):
        temp1 = ip_num % 10
        ip_num = ip_num // 10
# Check consecutive 0
        if (temp == True and temp1 == 0):
            return False
        if (temp1 > 0):
            temp = False
            continue
        temp = True
    return True


ip_num = int(input("Enter the number: "))
ip_base = int(input("Enter the base: "))
find(ip_num, ip_base)

Output

Program to check whether the given number has consecutive 0’s in the given base or not Output

Explanation

In the above program, we have created the function convert() and verify() to convert the number to base B and verify whether consecutive 0 exists. If the condition satisfies, the statement “Consecutive 0 exists!” is printed otherwise “Consecutive 0 does not exists!” is printed on the screen.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.