{"id":8083,"date":"2021-06-07T19:14:40","date_gmt":"2021-06-07T13:44:40","guid":{"rendered":"https:\/\/python-programs.com\/?p=8083"},"modified":"2021-11-22T18:38:37","modified_gmt":"2021-11-22T13:08:37","slug":"python-program-to-print-binary-representation-of-a-number","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-print-binary-representation-of-a-number\/","title":{"rendered":"Python Program to Print Binary Representation of a Number"},"content":{"rendered":"
Binary Representation:<\/strong><\/p>\n Binary (or base-2) is a numeral system with only two digits \u2014 0 and 1. Computers use binary to store data and execute calculations, which means they only use zeros and ones. In Boolean logic, a single binary digit can only represent True (1) or False (0). Any integer, in fact, can be represented in binary.<\/p>\n Given a decimal number the task is to convert the given decimal to binary<\/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 Example3:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n There are several ways to print the binary representation of a given decimal number some of them are:<\/p>\n Drive into Python Programming Examples<\/a> and explore more instances related to python concepts so that you can become proficient in generating programs in Python Programming Language.<\/p>\n To find a binary equivalent, divide the decimal number recursively by the value 2 until the decimal number hits zero. The remaining must be noted after each division. The binary equivalent of the decimal number is obtained by reversing the remaining values.<\/p>\n Algorithm:<\/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 Approach:<\/strong><\/p>\n i)Using replace function<\/strong><\/p>\n We will replace the 0b in the binary string with empty string.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n The binary representation of the given decimal number will be printed.<\/p>\n ii)Using slicing<\/strong><\/p>\n We will slice from 2 index to last index of the result returned by binary string<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n The binary representation of the given decimal number will be printed.<\/p>\n Related Programs<\/strong>:<\/p>\n Binary Representation: Binary (or base-2) is a numeral system with only two digits \u2014 0 and 1. Computers use binary to store data and execute calculations, which means they only use zeros and ones. In Boolean logic, a single binary digit can only represent True (1) or False (0). Any integer, in fact, can be …<\/p>\ngiven number =200<\/pre>\n
The binary representation of the given number 200 : \r\n11001000<\/pre>\n
given number =1<\/pre>\n
The binary representation of the given number 1: \r\n1<\/pre>\n
given number =32<\/pre>\n
The binary representation of the given number 32 : \r\n100000<\/pre>\n
Python Program to Print Binary Representation of a Number<\/h2>\n
\n
Method #1:Using recursive function<\/h3>\n
\n
def decitoBin(numb):\r\n # checking if the given number is greater than 1\r\n if numb > 1:\r\n # if it is greater than 1 then use recursive approach by dividing number by 2\r\n decitoBin(numb \/\/ 2)\r\n # printing the binary representation of the given number\r\n print(numb % 2, end='')\r\n\r\n\r\n# Driver code\r\ngiven_numb = 200\r\n# passing given number to decitoBin function to print binary representation of the givennumb\r\nprint(\"The binary representation of the given number\", given_numb, \" : \")\r\ndecitoBin(given_numb)\r\n<\/pre>\n
The binary representation of the given number 200 : \r\n11001000<\/pre>\n
Method #2:Using while loop<\/h3>\n
\n
def decitoBin(numb):\r\n # checking if the given number is greater than 1\r\n if numb > 1:\r\n # taking a empty string\r\n binastring = \"\"\r\n # looping till number greater than 0 using while loop\r\n while(numb > 0):\r\n # We will get the last check bit whether it is set bit or not using % operator\r\n checkbit = numb % 2\r\n # converting this checkbit to string using str() function\r\n checkbit = str(checkbit)\r\n # Concatenate this bit(can be 1 or 0 ) to the binstr.\r\n binastring = binastring+checkbit\r\n # divide the number by 2\r\n numb = numb\/\/2\r\n # reverse the binary string\r\n binastring = binastring[::-1]\r\n # return the resultant binary string\r\n return binastring\r\n\r\n\r\n# Driver code\r\ngiven_numb = 200\r\n# passing given number to decitoBin function to print binary representation of the givennumb\r\nprint(\"The binary representation of the given number\", given_numb, \" : \")\r\nprint(decitoBin(given_numb))\r\n<\/pre>\n
The binary representation of the given number 200 : \r\n11001000<\/pre>\n
Method #3:Using Built in Python function bin()<\/h3>\n
\n
def decitoBin(numb):\r\n # converting it to binary representation using bin() function\r\n binNumb = bin(numb)\r\n # replacing '0b' using replace function and replacing it with empty string\r\n binNumb = binNumb.replace('0b', '')\r\n # return the binary representation of the given number\r\n return binNumb\r\n\r\n\r\n# Driver code\r\ngiven_numb = 200\r\n# passing given number to decitoBin function to print binary representation of the givennumb\r\nprint(\"The binary representation of the given number\", given_numb, \" : \")\r\nprint(decitoBin(given_numb))\r\n<\/pre>\n
The binary representation of the given number 200 : \r\n11001000<\/pre>\n
def decitoBin(numb):\r\n # converting it to binary representation using bin() function\r\n binNumb = bin(numb)\r\n # We will slice from 2 index to last index of the result returned by binary string\r\n binNumb = binNumb[2:]\r\n # return the binary representation of the given number\r\n return binNumb\r\n\r\n\r\n# Driver code\r\ngiven_numb = 200\r\n# passing given number to decitoBin function to print binary representation of the givennumb\r\nprint(\"The binary representation of the given number\", given_numb, \" : \")\r\nprint(decitoBin(given_numb))\r\n<\/pre>\n
The binary representation of the given number 200 : \r\n11001000<\/pre>\n
\n