Saturday, 14 December 2024

Day 35 : Python Program to Print Binary Equivalent of an Integer using Recursion


def binary_equivalent(number):

    if number == 0:

        return ""

    return binary_equivalent(number // 2) + str(number % 2)

number = int(input("Enter an integer: "))

if number == 0:

    print("The binary equivalent of 0 is 0.")

elif number > 0:

    print(f"The binary equivalent of {number} is {binary_equivalent(number)}.")

else:

    print(f"The binary equivalent of {number} is -{binary_equivalent(abs(number))}.")

#source code --> clcoding.com

Code Explanation:

1. Recursive Function: binary_equivalent(number)
This function computes the binary representation of a number using recursion.
def binary_equivalent(number):
    if number == 0:
        return ""
    return binary_equivalent(number // 2) + str(number % 2)
Base Case:
if number == 0:
    return ""
If the input number is 0, the function returns an empty string (""). This is the stopping condition for recursion. The binary representation is built as the recursion unwinds.

Recursive Case:
return binary_equivalent(number // 2) + str(number % 2)
For numbers greater than 0:
Integer Division (number // 2): Divides the number by 2, reducing it in each recursive call.
Modulo (number % 2): Finds the remainder when divided by 2, which represents the current binary digit (0 or 1).
The result is the binary representation of number // 2 concatenated with the binary digit of the current step.

2. Taking Input

number = int(input("Enter an integer: "))
Prompts the user to enter an integer.
Converts the input string into an integer using int().

3. Handling Special Cases

if number == 0:
    print("The binary equivalent of 0 is 0.")

4. Positive Numbers

elif number > 0:
    print(f"The binary equivalent of {number} is {binary_equivalent(number)}.")
If the input is positive, the program calls binary_equivalent(number) and prints the result.

5. Negative Numbers

else:
    print(f"The binary equivalent of {number} is -{binary_equivalent(abs(number))}.")
If the input is negative:
The absolute value (abs(number)) is passed to the binary_equivalent function to compute the binary representation of the positive counterpart.
The program prepends a negative sign (-) to indicate the negative value.

 

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (87) AI (35) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (179) C (77) C# (12) C++ (83) Course (67) Coursera (231) Cybersecurity (24) Data Analytics (1) data management (11) Data Science (132) Data Strucures (8) Deep Learning (21) Django (14) Downloads (3) edx (2) Engineering (14) Euron (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Generative AI (4) Google (34) Hadoop (3) HTML Quiz (1) HTML&CSS (47) IBM (30) IoT (1) IS (25) Java (93) Java quiz (1) Leet Code (4) Machine Learning (62) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (4) Pandas (4) PHP (20) Projects (29) pyth (1) Python (958) Python Coding Challenge (398) Python Quiz (55) Python Tips (3) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (4) Software (17) SQL (42) UX Research (1) web application (8) Web development (4) web scraping (2)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses