๐Ÿ
B.TECH I YEAR โ€ข II SEMESTER โ€ข R25

Python Programming - Lab Manual

Python Programming Laboratory โ€ข Program Solution

๐Ÿ“˜ R25 Regulation ๐ŸŽ“ I Year ๐Ÿ“š II Semester ๐Ÿ Python Programming ๐Ÿงช Lab Program
Selected Program

binary strings of n-bit length

โ† All Python Programs

Aim:

☛   Writes a recursive function that generates all binary strings of n-bit length.

Solution :


PROGRAM: (binary_strings.py)

 
def generate_binary_number(length, binary_string):
    if len(binary_string) == length:
        print(binary_string)
        return

    generate_binary_number(length, binary_string + '0')
    generate_binary_number(length, binary_string + '1')


n = int(input("Enter number of bits to generate binary set = "))
print("Binary numbers are: ")
generate_binary_number(n, '')

OUTPUT:

 
 Enter number of bits to generate binary set = 3
Binary numbers are:
000
001
010
011
100
101
110
111


๐Ÿ’ป Run This Python Program

binary strings of n-bit length
The selected Python program is loaded automatically into the editor. Edit the program, provide input if required and click Run.

The compiler is embedded using OneCompiler. Your Python program runs inside the embedded Python environment; Study Glance does not execute Python code directly on your web server.
โ† Back to Python Lab Programs ๐Ÿ“š All Lab Manuals