This Python program receives the initial bank account balance, the annual interest rate, and a target amount.
It calculates how many years it will take for the balance to reach the target amount without any withdrawals.
Each year, the balance increases based on the interest rate.
def years_to_target(balance: float, interest_rate: float, target: float) -> int:
years = 0
while balance < target:
balance += balance * interest_rate / 100
years += 1
return years
# Read inputs from user
balance = float(input("Enter initial account balance: "))
interest_rate = float(input("Enter annual interest rate (%): "))
target = float(input("Enter target amount: "))
years = years_to_target(balance, interest_rate, target)
print(f"To reach {target:.2f}, it will take {years} years.")
To reach 200000.00, it will take 8 years.
- The user enters the initial balance, interest rate, and target amount
- The program uses a loop to simulate yearly growth
- Each year, the balance increases by the interest
- The loop continues until the balance reaches or exceeds the target
- The number of years is then displayed