Decimal to Binary Algorithm in Python5 Jan 2025 | 4 min read What 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 |
In Python, the Carriage Return (\r) is a special escape character used to move the cursor to the beginning of the current line without advancing to the line. It is commonly used in console output to overwrite the existing text, which is helpful for...
4 min read
This article focuses on calculating standard deviation in Python without using the NumPy library. It explores alternative methods, such as Python's built-in math library and statistics module, for both population and sample standard deviations. Practical examples and scenarios demonstrate the calculations, emphasizing manual approaches for greater...
5 min read
Introduction: Python offers a QQ plot, a graphical tool for comparing a dataset's distribution with a known theoretical one, enabling the determination of dataset in following a certain probability distribution. What is a Quantile-Quantile Plot? A quantile-quantile plot (or QQ plot) is a graphical tool used to compare the...
3 min read
In this problem we will be given a sentence which contains some words separated by spaces. Our task is to reverse each individual word of the sentence while maintaining the original order of words. Let us see some examples to understand the problem. Input: "We have...
5 min read
? Introduction Regular expressions, commonly referred to as regex or regexp, are sequences of characters that define a search pattern. They are used for string matching and manipulation, providing a powerful and flexible way to search, match, and edit text based on patterns. Regular expressions are widely...
4 min read
When we talk about scripting languages, we're referring to special types of computer languages used for specific purposes. Think of them as tools designed for particular tasks, like fixing a leaky faucet with a specific wrench rather than a general toolkit. Some of these scripting languages,...
25 min read
For this problem, we need to write a power function. We will be given two numbers, n and x, and we need to compute x ^ n. In this problem, we do not need to use the mod operation as we are assuming that the...
8 min read
? Introduction By overloading the less-than (__lt__) and greater-than (__gt__) operators in a class declaration in Python, you can create custom behavior for them. You can specify the ordering by which instances of your class shall be compared using these methods. Python calls these methods in order...
4 min read
? Introduction: In this tutorial we are learning about how to calculate a directory size using Python. A directory is defined as a collection of subdirectories and files. These subdirectories are separated in the directory hierarchy by using the "/" operator. Directory hierarchies are created by organizing...
6 min read
Overview of Clustering Among the most beneficial unsupervised machine learning techniques is Clustering. By using these techniques, data samples with similarity and relationship patterns are discovered, and the samples are subsequently clustered into groups based on shared characteristics. Because it establishes the inherent grouping among the current...
7 min read
We request you to subscribe our newsletter for upcoming updates.
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