In this program, you will learn to convert decimal number to binary using recursive function.
o understand this example, you should have the knowledge of the following Python programming topics:
Decimal number is converted into binary by dividing the number successively by 2 and printing the remainder in reverse order.
Source Code
# Function to print binary number using recursion def convertToBinary(n): if n > 1: convertToBinary(n//2) print(n % 2,end = '') # decimal number dec = 34 convertToBinary(dec) print()
Output
100010
You can change the variable dec in the above program and run it to test out for other values.
This program works only for whole numbers. It doesn’t work for real numbers having fractional values such as: 25.5, 45.64 and so on. We encourage you to create Python program that converts decimal numbers to binary for all real numbers on your own.
Related posts:
Python super()
Python ord()
Python String capitalize()
Python Machine Learning Cookbook - Practical solutions from preprocessing to Deep Learning - Chris A...
Python Program to Check If a List is Empty
Python sum()
Python Functions
Python Dictionary setdefault()
Python String lower()
Python String isupper()
Python Program to Create a Countdown Timer
Python Program to Display Fibonacci Sequence Using Recursion
Python Program to Merge Two Dictionaries
Python Program to Sort a Dictionary by Value
Python Program to Copy a File
Python Global Keyword
Deep Learning with Applications Using Python - Navin Kumar Manaswi
Python staticmethod()
Python Program to Add Two Matrices
Python Set symmetric_difference()
Machine Learning Mastery with Python - Understand your data, create accurate models and work project...
Python String isdigit()
Python tuple()
Python Deep Learning - Valentino Zocca & Gianmario Spacagna & Daniel Slater & Peter Roelants
Python List append()
Python help()
Python Program to Delete an Element From a Dictionary
Python Program to Print Hello world!
Python Program to Sort Words in Alphabetic Order
Python Program to Append to a File
Python Dictionary fromkeys()
Python Set copy()