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 |
Python has grown in popularity as one of the most popular programming languages today. Its simple syntax, along with a large number of libraries in general, makes it extremely versatile-a favorite amongst data engineers, especially for handling data jobs. In fact, many professionals turn to Python...
18 min read
In the following tutorial we will learn how to analyse Python program codes with the help of SonarQube. But before we get started, let us discuss the basics of SonarQube and the way of setting it up for analysing Python codes. An Introduction to SonarQube SonarQube is a...
5 min read
Introduction To make extend a list in Python, add new elements to the present list. This can be executed in a whole lot of ways, supplying versatility and effectiveness in various contexts. The extend() method, which appends data elements from another iterable to the list, is...
6 min read
In this article, you will learn how to create Boolean arrays and how to use them in your code. What is a Boolean Array? We all know arrays are collections of contiguous elements of the same type. Boolean arrays specifically store Boolean values ('true' and 'false'). Example: Boolean_array=[True, False,...
5 min read
The top ten incredible Python projects with source code are as follows: Random Story Generator Simple Email Slicer Acronyms Creator Body Mass Index(BMI) Calculator Dice Roll Simulator Basic Quiz Game Rock, Paper, and Scissors Game Chatbot Text to Speech Tic Tac Toe Project Random Story Generator It's one of the most fascinating source-code projects for beginners in...
17 min read
? Introduction Data manipulation is a crucial aspect of the data analysis process, and the ability to replace values in a pandas DataFrame based on certain conditions is a skill every data scientist and analyst should master. Pandas, a powerful and widely used data manipulation library in...
5 min read
In this problem, we are given a complete binary tree. A Complete binary tree has two subnodes for every node of the tree except for the leaf nodes. Our task is to count the total number of nodes that the given binary tree has. Let us...
7 min read
? In Python, lists are a versatile data structure used to store collections of items. Sometimes, when working with lists of strings, you may encounter situations where the list contains empty strings. Empty strings are strings that have no characters in them, represented by '' or...
4 min read
? Python has gotten to be a well-known programming dialect since of its ease to utilize, meaningfulness, and tremendous library. It has many uses outside of software creation, and it may be used to make money. Using Python's potential can result in profitable ventures, regardless of...
4 min read
? Introduction Python, a versatile and powerful programming language, provides robust tools for file handling. Reading specific lines from a file is a common requirement in various programming scenarios. Whether you are working with large datasets, log files, or configuration files, Python offers several efficient ways to...
4 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