You are on page 1of 8

Exp-4

Name:- Aayush Kumar

Roll no.:- 12112126

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

# Conversion factor from miles to kilometers


conversion_factor = 1.609

# Input from the user


miles = int(input("Enter the number of miles: "))

# 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))

# Print the result


print("The decimal equivalent of the hex character", hex_char, "is", decimal)

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

# Current tuition fee


current_tuition = int(input("Enter the current tuition fee: $"))

# Annual increase rate


increase_rate = 0.05

# Calculating the tuition fee after 10 years


tuition_after_10_years = current_tuition * (1 + increase_rate) ** 10

# Calculating the total cost of 4 years' worth of tuition starting 10 years from now
total_cost = tuition_after_10_years * 4

# Printing the results


print("The tuition fee after 10 years will be $%.2f" % tuition_after_10_years)
print("The total cost of 4 years' worth of tuition starting 10 years from now will be $%.2f" % total_cost)

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

# Counter for number of numbers displayed in a line


counter = 0

# Loop to check divisibility and print numbers


for num in range(100, 1001):
if num % 5 == 0 and num % 6 == 0:
print(num, end=" ")
counter += 1
if counter % 10 == 0:
print()

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

print("Interest Rate Monthly Payment Total Payment")


for i in range(5, 9):
interest_rate = i / 100
monthly_rate = interest_rate / 12
monthly_payment = loan_amount * monthly_rate / (1 - (1 / (1 + monthly_rate)**(loan_period * 12)))
total_payment = monthly_payment * loan_period * 12
print(f"{interest_rate:.2%} ${monthly_payment:.2f} ${total_payment:.2f}")

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

print("Payment # Interest Principal Balance")


for i in range(1, loan_period * 12 + 1):
interest = monthly_rate * balance
principal = monthly_payment - interest
balance = balance - principal
print(f"{i} ${interest:.2f} ${principal:.2f} ${balance:.2f}")

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()

# Print the result


print("The decimal equivalent of the hex character", hex_char, "is", decimal)

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:-

You might also like