0% found this document useful (0 votes)
6 views3 pages

Python Programs for Number Conversion

Uploaded by

naikerfan3
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Python Programs for Number Conversion

Uploaded by

naikerfan3
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python Lab 3

1. Write a program to Convert a binary number taken as user input into its decimal
equivalent.
User Input: 101010
Output: 42
def binarytodecimal(bin):
dec = 0;
for i in range(len(bin)):
dec *= 2;
dec +=int(bin[i])
return dec

bin ="1010"
print(binarytodecimal(bin))
Output:

2. Write the Python code and using Recursion/Iterations solve the problem stated in
Program 2.

def binary_to_decimal_recursive(binary):
if len(binary) == 0:
return 0

return int(binary[0]) * (2 ** (len(binary) - 1)) +


binary_to_decimal_recursive(binary[1:])

binary_input = input("Enter a binary number: ")

decimal_output = binary_to_decimal_recursive(binary_input)

print(f"The decimal equivalent of binary {binary_input} is: {decimal_output}")

Output:

3. Given the decimal number 168, represent it using octal and hexadecimal literals.

def decimal_to_octal(decimal):
return oct(decimal)[2:]
def decimal_to_hexadecimal(decimal):
return hex(decimal)[2:]

decimal_number = int(input("Enter a decimal number: "))

octal_value = decimal_to_octal(decimal_number)
hexadecimal_value = decimal_to_hexadecimal(decimal_number)

print(f"Octal value of {decimal_number} is {octal_value}")


print(f"Hexadecimal value of {decimal_number} is {hexadecimal_value}")

Output:

4. Write a Python Program (to implement the concept of function overloading) having 1
function ‘calculate price’ that calculates the final price of an item after applying the
discount.
If only the original price is provided by the user, calculate the price after applying
10 % discount.
If the original price and a discount percentage are provided by the user, calculate the
price after applying the provided Discount.

def calculate_price(price, discount=10):

finalprice = price - (price * discount / 100)


return finalprice

originalrice = 1000

print("Final price (with default 10% discount):", calculate_price(originalrice))

customDiscount = 15
print("Final price (with 15% discount):", calculate_price(originalrice,
customDiscount))

Output:

5. Write a Python program that takes a floating-point value as input from the user and
outputs the binary equivalent of that number (up to 10 digits).
For example:
Input: 2.2
Output: 10.00110011000
def float_to_binary(num):
intpart = int(num)
fractional_part = num - intpart

integer_binary = bin(intpart)[2:]

fractional_binary = ""
while fractional_part and len(fractional_binary) < 10:
fractional_part *= 2
bit = int(fractional_part)
fractional_binary += str(bit)
fractional_part -= bit

return integer_binary + '.' + fractional_binary

number = float(input("Enter a floating-point number: "))


binary_output = float_to_binary(number)
print(f"Binary equivalent: {binary_output}")

Output:

You might also like