In: Computer Science
1. Write Python program that use a while loop to determine how long it takes for an investment to double at a given interest rate.
The input will be an annualized interest rate, and the output is the number of years it takes an investment to double.
Note: The amount of initial investment can be any positive value, you can use $1.
2. Write a function def DoubleInvest(initialInvest) to perform the same functionalities as question1. Make sure to include you main function for testing.
Given below is the code for question. Please indent code as shown in image.
Answer 1)
=====
initialAmt = 1
rate = float(input('Enter interest rate: '))
finalAmount = initialAmt
years = 0
while finalAmount < 2 * initialAmt:
years = years + 1
interest = finalAmount * rate / 100.0
finalAmount = finalAmount + interest
print('At an interest rate of {:.2f}, your initial investment of ${:.2f} will be ${:.2f} in {} years'.format(rate, initialAmt, finalAmount, years))
Answer 2)
=====
def DoubleInvest(initialInvest):
rate = float(input('Enter interest rate: '))
finalAmount = initialInvest
years = 0
while finalAmount < 2 * initialInvest:
years = years + 1
interest = finalAmount * rate /
100.0
finalAmount = finalAmount +
interest
print('At an interest rate of {:.2f}, your initial investment of ${:.2f} will be ${:.2f} in {} years'.format(rate, initialInvest, finalAmount, years))
def main():
initialInvest = 1
DoubleInvest(initialInvest)
if __name__ == "__main__":
main()
output
------
Enter interest rate: 5
At an interest rate of 5.00, your initial investment of $1.00 will
be $2.08 in 15 years