Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Name: Zeyad Mustafa Younis Mahmoud

Group: 2
Assignment 2
Exercise 2: Analyze the following fragment:
1.First Fragment:
sum = d = 0
while d != 10.0:
d += 0.1
sum += sum + d

The correct answer is:


(C) The program may not stop because of the phenomenon referred to as numerical
inaccuracy for operating with floating-point numbers.

2.Second Fragment:
count = 0

while count < 100:


# Point A
print("Welcome to Python!")
count += 1
# Point B

# Point C

The correct answer is:


(E) count < 100 is always False at Point C
3.Third Fragment:
sum = 0

for d in range(0, 10, 0.1):


sum += sum + d

The correct answer is:


(A)The program has a syntax error because the range function cannot have three
arguments.
Exercise 4: (Conversion from kilograms to pounds) Write a program that displays the
following table (note that 1 kilogram is 2.2 pounds):

print("Kilograms Pounds")
print(“----------------")

for kilograms in range(1, 200, 2):


pounds = kilograms * 2.2
print(f'{kilograms:‹ 11} {pounds:<.1f}")

When we run this program, it should output the desired table:


Kilograms Pounds
1 2.2
3 6.6
...
197 433.4
199 437.8

Exercise 6: (Find numbers divisible by 5 and 6) Write a program that displays, ten numbers
per line, all the numbers from 100 to 1,000 that are divisible by 5 and 6. The numbers are
separated by exactly one space.

count = 0 # Initialize count for formatting


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

print() # Move to the next line


count = 0 # Reset count for the next line

Exercise 8: Write a Python program to find the factorial of a given number:


def factorial(n):
if n< 0:
return "Factorial is undefined for negative numbers"
elif n == 0 or n == 1: return 1 else:
result = 1
for i in range(2, n + 1):
result *= i
return result
number = int(input("Enter a non-negative integer: "))
result = factorial(number)
print(f" The factorial of {number} is: {result}")

Exercise 10: (Financial application: loan amortization schedule) The monthly payment for
a given loan pays the principal and the interest. The monthly interest is computed by
multiplying the monthly interest rate and the balance (the remaining principal). The
principal paid for the month is therefore the monthly payment minus the monthly interest.
Write a program that lets the user enter the loan amount, number of years, and interest
rate, and then displays the amortization schedule for the loan.

def calculate_amortization_schedule(loan _amount, num _years, annual_interest _rate):


monthly_interest_rate = annual_interest_rate / 100 / 12
num_payments = num_years * 12
monthly _payment = (loan _amount * monthly _interest _rate) / (1- (1 +
monthly_interest_rate) ** -num_payments)

balance = loan_amount

print("Payment# Interest Principal Balance”)

for payment_num in range(1, num_payments + 1):


interest_payment = balance * monthly_interest_rate

principal_payment = monthly_payment - interest_payment


balance -= principal_payment
print(f"{payment_num:<9}{interest_payment:2f} {principal_payment:2f} {balance:
2f}")

loan_amount = float(input("Enter the loan amount: "))


num_years = int(input("Enter the number of years:"))
annual_interest _rate = float(input"Enter the annual interest rate (in percentage):”))
calculate _amortization_schedule(loan amount, num _years, annual interest rate)

You might also like