
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python program to convert decimal to binary number
In this article, we will learn about the solution and approach to solve the given problem statement.
Problem statement
Given a number we need to convert into a binary number.
Approach 1 − Recursive Solution
DecToBin(num): if num > 1: DecToBin(num // 2) print num % 2
Example
def DecimalToBinary(num): if num > 1: DecimalToBinary(num // 2) print(num % 2, end = '') # main if __name__ == '__main__': dec_val = 35 DecimalToBinary(dec_val)
Output
100011
All the variables and functions are declared in the global scope as shown below −
Approach 2 − Built-in Solution
Example
def decimalToBinary(n): return bin(n).replace("0b", "") # Driver code if __name__ == '__main__': print(decimalToBinary(35))
Output
100011
All the variables and functions are declared in the global scope as shown below −
Conclusion
In this article, we learnt about the approach to convert a decimal number to a binary number.
- Related Articles
- Convert decimal to binary number in Python program
- Java Program to convert binary number to decimal number
- C++ Program To Convert Decimal Number to Binary
- Java program to convert decimal number to binary value
- Java program to convert binary number to decimal value
- Program to convert Linked list representing binary number to decimal integer in Python
- C++ program to Convert a Decimal Number to Binary Number using Stacks
- Haskell program to convert a decimal number into a binary number
- C++ Program to Convert Binary Number to Decimal and vice-versa
- Swift program to convert the decimal number to binary using recursion
- C# Program to Convert Binary to Decimal
- Swift Program to convert Decimal to Binary
- Swift Program to convert Binary to Decimal
- Haskell Program to convert Decimal to Binary
- Haskell Program to convert Binary to Decimal

Advertisements