Professional Documents
Culture Documents
Section:- CSB-06
Q1)
# Take input of hex character from the user
hex_char = input("Enter a hex character: ")
# Convert the hex character to decimal using the built-in int() function with base 16
decimal = int(hex_char, 16)# This program takes the number of miles as input from the user and converts it to
kilometers
# Table header
print("\n Miles \t Kilometers")
# Loop to calculate and print the conversion for the specified number of miles
for i in range(1, miles + 1):
kilometers = i * conversion_factor
print("{:<8} {:<14.3f}".format(i, kilometers))
Output:-
Q2)
# This program calculates the tuition fee after 10 years and the total cost of 4 years' worth of tuition starting 10
years from now
# Calculating the total cost of 4 years' worth of tuition starting 10 years from now
total_cost = tuition_after_10_years * 4
Output:-
Q3)
# This program displays ten numbers per line that are divisible by 5 and 6 and lie in the range from 100 to 1000
Output:-
Q4)
def display_ascii_table():
for i, c in enumerate(range(33, 127)):
print(chr(c), end=' ')
if (i + 1) % 10 == 0:
print()
display_ascii_table()
Output:-
Q5)
# Pattern A
for i in range(1, 7):
for j in range(1, i+1):
print(j, end=" ")
print()
print("\n")
# Pattern B
for i in range(6, 0, -1):
for j in range(1, i+1):
print(j, end=" ")
print()
print("\n")
# Pattern C
for i in range(1, 7):
for j in range(6, 0, -1):
if j > i:
print(" ", end="")
else:
print(j, end=" ")
print()
print("\n")
# Pattern D
for i in range(6, 0, -1):
for j in range(1, i+1):
print(j, end=" ")
for k in range(i, 6):
print(" ", end=" ")
print()
Output:-
Q6)
def display_pattern():
for i in range(1, 7):
for j in range(7-i):
print(" ", end='')
for k in range(i):
print(2**k, end=' ')
for l in range(i-1, 0, -1):
print(2**(l-1), end=' ')
print()
display_pattern()
Output:-
Q7)
def loan_calculator():
loan_amount = float(input("Enter loan amount: $"))
loan_period = float(input("Enter loan period in number of years: "))
loan_calculator()
Output:-
Q8)
def loan_amortization():
loan_amount = float(input("Enter loan amount: $"))
loan_period = int(input("Enter loan period in number of years: "))
interest_rate = float(input("Enter interest rate (%): ")) / 100
monthly_rate = interest_rate / 12
monthly_payment = loan_amount * monthly_rate / (1 - (1 / (1 + monthly_rate) ** (loan_period * 12)))
balance = loan_amount
if balance > 0:
last_payment = monthly_payment + balance
print(f"{loan_period * 12 + 1} ${interest:.2f} ${last_payment:.2f} $0.00")
loan_amortization()
Output:-
Q9)
def perfect_numbers():
count = 0
for num in range(1, 10000):
divisors = [x for x in range(1, num) if num % x == 0]
if sum(divisors) == num:
count += 1
print(num)
if count == 4:
break
perfect_numbers()
Output:-
Q10)
def combinations():
count = 0
for i in range(1, 8):
for j in range(i + 1, 8):
count += 1
print(i, j)
print("The total number of all combinations is", count)
combinations()
Output:-