Python Program to Check a Binary Number is Divisible by a Number N

In the previous article, we have discussed Python Program to Pick a Random Card
Binary Number:

A binary number is a number expressed in the base 2 numeral system, which employs only the symbols 0 and 1.

Conversion from binary to decimal:

binary number = 1010

decimal number = int(str(binary number),2)

Given a binary number, and the task is to check if the binary number is divisible by a given number N.

Examples:

Example1:

Input:

Given binary number = 1100
Given number = 4

Output:

The given binary number is divisible by{ 4 }

Example2:

Input:

Given binary number = 1000
Given number = 2

Output:

The given binary number is divisible by{ 2 }

Program to Check a Binary Number is Divisible by a number N.

Below are the ways to check if the given binary number is divisible by a given number N.

Method #1: Using Built-in Functions (Static input)

Approach:

  • Give the binary number as static input and store it in a variable.
  • Given the number as static input and store it in another variable.
  • Convert the given binary number into a decimal number using int(str(binary number),2) function and store it in another variable say “deci”.
  • Check if the above-obtained decimal number modulus given number is equal to 0 using the if conditional statement.
  • If the statement is true, then print “The binary number is divisible by the given input number”.
  • Else print “The binary number is not divisible by the given input number”.
  • The Exit of the program.

Below is the implementation:

# Give the binary number as static input and store it in a variable.
binry = 1100
# Given the number as static input and store it in another variable.
num = 4
# Convert the given binary number into a decimal number using int(str(binary number),2)
# function and store it in another variable say "deci".
deci = int(str(binry), 2)
# Check if the above-obtained decimal number modulus given number is equal to 0 using the if
# conditional statement.
if deci % num == 0:
    # If the statement is true, then print "The binary number is divisible by the given
    # input number".
    print("The given binary number is divisible by{", num, "}")
else:
 # Else print ""The binary number is not divisible by the given input number".
    print("The given binary number is not divisible by{", num, "}")

Output:

The given binary number is divisible by{ 4 }

Method #2: Using Built-in Functions (User input)

Approach:

  • Give the binary number as user input using int(input()) and store it in a variable.
  • Given the number as user input using int(input()) and store it in another variable.
  • Convert the given binary number into a decimal number using int(str(binary number),2) function and store it in another variable say “deci”.
  • Check if the above-obtained decimal number modulus given number is equal to 0 using the if conditional statement.
  • If the statement is true, then print “The binary number is divisible by the given input number”.
  • Else print “The binary number is not divisible by the given input number”.
  • The Exit of the program.

Below is the implementation:

# Give the binary number as user input using int(input()) and store it in a variable.
binry = int(input("Enter some random number = "))
# Given the number as user input using int(input()) and store it in another variable.
num = int(input("Enter some random number = "))
# Convert the given binary number into a decimal number using int(str(binary number),2)
# function and store it in another variable say "deci".
deci = int(str(binry), 2)
# Check if the above-obtained decimal number modulus given number is equal to 0 using the if
# conditional statement.
if deci % num == 0:
    # If the statement is true, then print "The binary number is divisible by the given
    # input number".
    print("The given binary number is divisible by{", num, "}")
else:
 # Else print ""The binary number is not divisible by the given input number".
    print("The given binary number is not divisible by{", num, "}")

Output:

Enter some random number = 1000
Enter some random number = 2
The given binary number is divisible by{ 2 }

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.