0% found this document useful (0 votes)
11 views2 pages

PART B Python

The document contains Python programs for three tasks: converting a decimal number to binary, converting integers to Roman numerals using dictionaries, and recognizing a specific phone number pattern using regular expressions. Each task is implemented with a function and includes user input for testing. The code snippets demonstrate basic programming concepts and the use of libraries like 're' for pattern matching.

Uploaded by

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

PART B Python

The document contains Python programs for three tasks: converting a decimal number to binary, converting integers to Roman numerals using dictionaries, and recognizing a specific phone number pattern using regular expressions. Each task is implemented with a function and includes user input for testing. The code snippets demonstrate basic programming concepts and the use of libraries like 're' for pattern matching.

Uploaded by

Nidhi Rao
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

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.")

You might also like