{"id":17197,"date":"2021-08-26T10:42:58","date_gmt":"2021-08-26T05:12:58","guid":{"rendered":"https:\/\/python-programs.com\/?p=17197"},"modified":"2021-11-22T18:37:10","modified_gmt":"2021-11-22T13:07:10","slug":"python-program-to-check-a-binary-number-is-divisible-by-a-number-n","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-check-a-binary-number-is-divisible-by-a-number-n\/","title":{"rendered":"Python Program to Check a Binary Number is Divisible by a Number N"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Pick a Random Card<\/a> A binary number is a number expressed in the base 2 numeral system, which employs only the symbols 0 and 1.<\/p>\n Conversion from binary to decimal:<\/strong><\/p>\n binary number = 1010<\/p>\n decimal number = int(str(binary number),2)<\/strong><\/p>\n Given a binary number, and the task is to check if the binary number is divisible by a given number N.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to check if the given binary number is divisible by a given number N.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explore more instances related to python concepts from\u00a0Python Programming Examples<\/a>\u00a0Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\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, …<\/p>\n
\nBinary Number:<\/strong><\/p>\nGiven binary number = 1100\r\nGiven number = 4<\/pre>\n
The given binary number is divisible by{ 4 }<\/pre>\n
Given binary number = 1000\r\nGiven number = 2<\/pre>\n
The given binary number is divisible by{ 2 }<\/pre>\n
Program to Check a Binary Number is Divisible by a number N.<\/h2>\n
\n
Method #1: Using Built-in Functions (Static input)<\/h3>\n
\n
# Give the binary number as static input and store it in a variable.\r\nbinry = 1100\r\n# Given the number as static input and store it in another variable.\r\nnum = 4\r\n# Convert the given binary number into a decimal number using int(str(binary number),2)\r\n# function and store it in another variable say \"deci\".\r\ndeci = int(str(binry), 2)\r\n# Check if the above-obtained decimal number modulus given\u00a0number is equal to 0 using the if\r\n# conditional statement.\r\nif deci % num == 0:\r\n # If the statement is true, then print \"The binary number is divisible by the given\r\n # input number\".\r\n print(\"The given binary number is divisible by{\", num, \"}\")\r\nelse:\r\n # Else print \"\"The binary number is not divisible by the given input number\".\r\n print(\"The given binary number is not divisible by{\", num, \"}\")\r\n<\/pre>\n
The given binary number is divisible by{ 4 }<\/pre>\n
Method #2: Using Built-in Functions (User input)<\/h3>\n
\n
# Give the binary number as user input using int(input()) and store it in a variable.\r\nbinry = int(input(\"Enter some random number = \"))\r\n# Given the number as user input using int(input()) and store it in another variable.\r\nnum = int(input(\"Enter some random number = \"))\r\n# Convert the given binary number into a decimal number using int(str(binary number),2)\r\n# function and store it in another variable say \"deci\".\r\ndeci = int(str(binry), 2)\r\n# Check if the above-obtained decimal number modulus given\u00a0number is equal to 0 using the if\r\n# conditional statement.\r\nif deci % num == 0:\r\n # If the statement is true, then print \"The binary number is divisible by the given\r\n # input number\".\r\n print(\"The given binary number is divisible by{\", num, \"}\")\r\nelse:\r\n # Else print \"\"The binary number is not divisible by the given input number\".\r\n print(\"The given binary number is not divisible by{\", num, \"}\")\r\n<\/pre>\n
Enter some random number = 1000\r\nEnter some random number = 2\r\nThe given binary number is divisible by{ 2 }<\/pre>\n
\n