Part of the series

Several example codes

~1 min read • Updated Oct 12, 2025

Program Overview

This Python program first prints the numbers from 1 to 10.
Then, it converts the number 11 from base 10 to base 2 using a recursive function.
The recursive function divides the number by 2 and builds the binary string from the remainders.


Python Code:


def decimal_to_binary(n: int) -> str:
    if n == 0:
        return ""
    return decimal_to_binary(n // 2) + str(n % 2)

# Print numbers from 1 to 10
print("Numbers from 1 to 10:")
for i in range(1, 11):
    print(i, end=" ")
print("\n")

# Convert 11 to binary
x = 11
binary = decimal_to_binary(x)
binary = binary if binary else "0"
print(f"The binary representation of {x} is: {binary}")

Sample Output:


Numbers from 1 to 10:
1 2 3 4 5 6 7 8 9 10

The binary representation of 11 is: 1011

Written & researched by Dr. Shahin Siami