Examples
- Hello World Program in Python (How to Print Hello World in Python?)
- Python Program to Add Two Numbers (Sum/Addition Program)
- How to Generate Random Number in Python? (Program)
- Leap Year Program in Python (Check Leap Year or Not in Python)
- Python Program to Check Number is Positive, Negative, or Zero
- Even Odd Program in Python (How to Check Number is Even or Odd?)
- Python Program to Find Largest of 3 Numbers (Greatest of Three Numbers in Python)
- Prime Number Program in Python (Check Prime or Not)
- Python Program to Find Square Root of a Number (Examples)
- Python Factorial Program (How to Find Factorial of a Number in Python?)
- Python Program to Find Prime Numbers in Range
- Python Program to Find Area of Triangle (Different Ways With Examples)
- How to Solve Quadratic Equation in Python? (Program Examples)
- How to Swap Two Numbers in Python? (Program Examples to Swap Variables)
- Python Program to Convert Kilometers to Miles (km to mi) and Vice Versa
- Python Program to Convert Celsius To Fahrenheit (and Fahrenheit to Celsius)
- Program to Check Armstrong Numbers in Python (Code Examples)
- Program for Armstrong Number Between Two Intervals in Python
- Program to Print Multiplication Table in Python (Code Examples)
- Python Program for Fibonacci Series (Print Fibonacci Sequence in Python)
- Program for Sum of n Natural Numbers in Python (Code Examples)
- Program to Print Powers of 2 in Python (Code Examples)
- Python Program to Find Numbers Divisible by Another Number
- Convert Decimal to Binary, Octal and Hexadecimal in Python (Program with Example)
- How to Convert Decimal to Binary in Python? Program with Examples
- How to Convert Decimal to Octal in Python? Program with Examples
- How to Convert Decimal to Hexadecimal in Python? Program and Examples
- How to Convert Binary to Decimal in Python? Program With Examples
- How to Convert Hexadecimal to Octal in Python? Program With Examples
- Program to Find LCM of Two Numbers in Python (Calculate LCM in Python)
- GCD of Two Numbers in Python (Program to Find HCF or GCD)
- GCD of Three Numbers in Python (HCF Program for n Numbers)
- Simple Calculator Program in Python (Basic Calculator Code)
- Find Factors of a Number in Python (Programs and Examples)
- Program to Find Prime Factors of a Number in Python
- How to Find ASCII Value in Python? ASCII Value of Character
How to Convert Decimal to Binary in Python? Program with Examples
Numbers are the building blocks of information and data representation in computer science. While the decimal system, based on the number 10, is widely used in our daily lives, computer systems primarily operate using the binary system, which is based on the number 2.
Binary numbers consist of only two digits, 0 and 1, and form the foundation of all digital systems. Understanding how to convert decimal to binary in Python is a fundamental skill for any programmer, as it allows for efficient storage, manipulation, and communication of data within computer systems.
Mastering this conversion process is essential in various programming domains, such as working with low-level programming, bitwise operations, data compression, cryptography, and network protocols. It empowers you to analyze and manipulate binary data effectively, making you more proficient in understanding the inner workings of computer systems.
In this tutorial, we will explore the decimal to binary program in Python, both with and without inbuilt function. It will help you to gain a clear understanding of the concept and have a practical tool to perform decimal to binary conversion in your own Python projects.
Decimal to Binary in Python (With In-built Function)
Code
# Main program
if __name__ == "__main__":
decimal = int(input("Enter a decimal number: "))
binary = bin(decimal).replace("0b", "")
print("Binary: " + binary)
Output
Enter a decimal number: 100
Binary: 1100100
Explanation
Here, the bin() function is used to convert the decimal number to its binary representation.
The replace() method removes the prefix "0b" from the result to obtain the binary string.
Decimal to Binary in Python (Without In-built Function)
Code
# Function to convert decimal to binary
def decimal_to_binary(decimal):
binary = ""
while decimal > 0:
binary = str(decimal % 2) + binary
decimal = decimal // 2
return binary
# Main program
if __name__ == "__main__":
decimal = int(input("Enter a decimal number: "))
binary = decimal_to_binary(decimal)
print("Binary: " + binary)
Output
Enter a decimal number: 756
Binary: 1011110100
Explanation
We define a function decimal_to_binary() that takes a decimal number as input and returns its binary representation.
Inside the function, we initialize an empty string binary to store the binary digits.
Using a while loop, we repeatedly perform the following steps until the decimal number becomes zero:
-
We compute the remainder when the decimal number is divided by 2 using the modulus operator %.
-
We convert the remainder to a string and concatenate it with the left side of the binary string.
-
We update the decimal number by performing integer division by 2 (decimal // 2).
After the while loop ends, we have obtained the binary representation of the decimal number. We return the binary string.
In the main program, we prompt the user to enter a decimal number using the input() function, and we convert it to an integer using int().
We call the decimal_to_binary() function with the decimal number as an argument and store the result in the binary variable.
Finally, we print the binary representation of the decimal number using the print() function, along with an appropriate label.
By running this program, you can convert any decimal number into its binary representation in Python.
Decimal to Binary 16 bit Conversion (Leading Zeros)
Below is a Python program to convert a decimal to binary, ensuring that the binary output is represented using 16 bits:
Code
def decimal_to_binary(decimal):
binary = format(decimal, '016b')
return binary
if __name__ == "__main__":
decimal = int(input("Enter a decimal number: "))
binary = decimal_to_binary(decimal)
print("Binary (16-bit): " + binary)
Output
Enter a decimal number: 635
Binary (16-bit): 0000001001111011
Explanation
-
The decimal_to_binary() function takes a decimal number as input and returns its binary representation as a string.
-
Inside the function, we use the format() function with the format specifier '016b' to convert the decimal number to its binary with leading zeros, ensuring a 16-bit output.
-
The format specifier '016b' represents a 16-bit binary number, where 0 denotes leading zeros, 16 denotes the width of the binary output, and b indicates the binary format.
-
Finally, we return the resulting binary string.
-
In the main program, we prompt the user to enter a decimal number using the input() function, and we convert it to an integer using int().
-
We call the decimal_to_binary() function with the decimal number as an argument and store the result in the binary variable.
-
Finally, we print the binary representation of the decimal number using the print() function, along with an appropriate label.
Practice More Python Programs:
- Hello World Program in Python
- Python Program to Add Two Numbers
- Generate Random Number in Python
- Leap Year Program in Python
- Even Odd Program in Python
- Python Program to Find Largest of 3 Numbers
- Python Program to Find Square Root of a Number
- Python Factorial Program
- Python Program to Find Area of Triangle
- Solve Quadratic Equation in Python
- Swap Two Numbers in Python
- Python Program to Convert Kilometers to Miles
- Python Program to Convert Celsius To Fahrenheit
- Check Armstrong Numbers in Python
- Print Multiplication Table in Python
- Python Program for Fibonacci Series
- Convert Decimal to Binary, Octal and Hexadecimal in Python
- Find LCM of Two Numbers in Python
- GCD of Two Numbers in Python
- Simple Calculator Program in Python
- Python Matrix Addition Program
- Transpose of Matrix in Python
- Python Matrix Multiplication
- Python Calendar Program