This is a Python Program to find the binary equivalent of a number recursively.
The program takes a number and finds the binary equivalent of a number recursively.
1. Define a recursive function which takes a number as the argument.
2. Take a number from the user and pass it as an argument to a recursive function.
3. In the function, put the base condition that if the number is zero, return the formed list.
4. Otherwise, convert each digit into binary and append it to the list.
5. Reverse the list and using a for loop print the elements of the list.
6. Exit.
Here is source code of the Python Program to find the binary equivalent of a number recursively. The program output is also shown below.
l=[] def convert(b): if(b==0): return l dig=b%2 l.append(dig) convert(b//2) a=int(input("Enter a number: ")) convert(a) l.reverse() print("Binary equivalent:") for i in l: print i,
1. A recursive function is defined which takes a number as the argument.
2. A number is taken from the user and passed as an argument to a recursive function.
3. In the function, the base condition is that if the number is zero, the formed list is returned.
4. Otherwise, each digit is converted into binary and appended to the list.
5. The list is reversed and a for loop is used to print the elements of the list.
Case 1: Enter a number: 20 Binary equivalent: 1 0 1 0 0 Case 2: Enter a number: 7 Binary equivalent: 1 1 1
Sanfoundry Global Education & Learning Series – Python Programs.
To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.
- Apply for Python Internship
- Practice Programming MCQs
- Check Information Technology Books
- Check Python Books
- Apply for Programming Internship