PART B
1. Create a Python program to convert a decimal number to binary using a function
def decimal_to_binary(decimal):
binary = ""
while decimal > 0:
binary = str(decimal % 2) + binary
decimal = decimal // 2
return binary
decimal_input = int(input("Enter a decimal number: "))
binary_result = decimal_to_binary(decimal_input)
print(f"The binary representation of {decimal_input} is
{binary_result}")
2.Write a program to convert integer values in to roman numbers using dictionaries
def int_to_roman(num):
roman_numerals = { 1: 'I', 4: 'IV', 5: 'V', 9:
'IX', 10: 'X', 40: 'XL', 50: 'L', 90: 'XC',
100: 'C', 400: 'CD', 500: 'D', 900: 'CM',1000: 'M' }
result = ""
for value in sorted(roman_numerals.keys(),
reverse=True):
while num >= value:
result += roman_numerals[value]
num -= value
return result
integer_input = int(input("Enter an integer: "))
roman_numeral = int_to_roman(integer_input)
print(f"The Roman numeral for {integer_input} is:
{roman_numeral}")
3.Write a function called isphonenumber () to recognize a pattern
1111-252-4545 using regular expression
import re
def isphonenumber(text):
pattern = r'\d{4}-\d{3}-\d{4}'
match = re.search(pattern, text)
if match:
return True
else:
return False
user_input = input("Enter text to check for a phone
number: ")
if isphonenumber(user_input):
print("Found a phone number:", user_input)
else:
print("No phone number found in the input.")