Home > Software design >  Find number of years it takes for an amount of money in a bank to pass a certain number with yearly
Find number of years it takes for an amount of money in a bank to pass a certain number with yearly

Time:10-18

First of January each year, Keven puts an amount of 10000 in a bank with an interest rate of 1.8 %. The first deposit was 01.01.2020. Make a Python program that calculates the number of years it takes for the amount of money to pass a certain number K. The input should be yearly deposit, interest rate, and K.The output should number of years.

I tried with while loop but got infinite loop.

P=1.8      # Interest rate
D=10000     # Yearly deposit
K=250000
n=0         # n:number of years
A=D         # A:amount of money in the bank
while A<=K:
 n=n 1
 D= A
 A=D*(1 1.8/100)  # Exponential growth
print(n)

CodePudding user response:

If deposit is added before calculation of interest (as your code shows):

while A <= K:
    n =1
    A = (A   D) * (1   P/100)

If deposit is added after calculation of interest:

while A <= K:
    n =1
    A = A * (1   P/100)   D

both variants do work (they give same answer 20 for given data but differ for another input)

Your mistake is here

D= A 
# but yearly deposit is constant

A=D*(1 1.8/100)  # Exponential growth
#we should update current amount A with interest
  • Related