Decimal to Binary Algorithm in PythonWhat are Decimal Numbers?Decimal Numbers are the number system that uses 10 digits, from 0 to 9. The base of the decimal number system is 10. It is also known as the base-10 number system. It is used to form digits with different combinations. Each digit becomes 10 times more significant in the decimal numbers while moving from the unit place. For example: (345)10 = 3 x 102 + 4 x 101 + 5 x 100 What are Binary Numbers?Binary numbers use two digits, 0 and 1. The base of the binary numbers is 2. It is also known as the base-2 number system. The combination of the binary digits is used to form different numbers. Each digit is 2 times more significant in binary numbers starting from the unit place. For example: (11011)2 = 1 x 24 + 1 x 23 + 0 x 22 + 1 x 21 + 1 x 20 Converting Decimal to Binary Numbers in PythonProblem StatementWe have decimal numbers and need to convert them into binary numbers using Python. Let's understand the problem statement using examples. Example 1: Input: Output: 1111011 Example 2: Input: Output: 10001110 There are different approaches to converting decimal numbers to binary numbers. Approach 1: RecursionAlgorithm:
Program: Code: Output: Enter the decimal number: 123 1 1 1 1 0 1 1 Explanation: We made a function DecToBin( ), which recursively converts the decimal number to binary. It will first check if the number is greater than 1. Then, it recursively calls the n // 2. Then, it will print the binary equivalent of the number (n % 2). It will take the number from the user and call the function, which converts the decimal to binary number. Approach 2: Built-in Function to convert decimal to binaryAlgorithm:
Program: Code: Output: Enter a decimal number : 123 1111011 Explanation: We made a function, DecToBin( ), which converts the decimal number to a binary number. Then, using the built-in bin( ) function, we converted the decimal number to a binary number. We then called the function and printed the output. Approach 3: Using the built-in format( ) function.Algorithm:
Program: Code: Output: Enter a decimal number : 1445 10110100101 Explanation: We made a function, DecToBin( ), which converts the decimal number to a binary number. Then, we used the format( ) function with argument {0:b}. We take the input from the user and call the function to print the output. Or We can use the format function in another way: Program: Code: Output: Enter a decimal number : 198 11000110 Explanation: We first made the DecToBin () function to convert the decimal to a binary number. Then, using the format( ) function, we converted the decimal number to a binary number. We take the input from the user and call the function to print the output. Approach 4: One Line code to convert decimal numbers to binary numbersAlgorithm: This is a one-liner code to convert decimal to binary numbers. It is also known as the Quick Ninja Method. Program: Code: Output: Enter a decimal number : 122345 11101110111101001 Explanation: We used the quick ninja method to convert decimal numbers to binary numbers. We take the input from the user and use the bin( ) function to convert the decimal number to a binary number. Approach 5: Bitwise Shift Operator to convert Decimal to Binary numberAlgorithm:
Code: Output: Enter a decimal number : 1998 11111001110 Explanation: We made a function, DecToBin( ), which converts the decimal number to a binary number. Then, we initiated a while loop in which the decimal number is converted to a binary number using the bitwise right shift operator. Then, we take input from the user and call the function to print the output. Next TopicDeep-learning-algorithms-in-python |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India