Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Program to find how many years it will take to reach t amount in Python
Suppose we have some parameters P,O,E,T. If we have P dollars in principal that we want to invest the stock market. The stock market alternates between first returning E and then O percent interest per year, we have to check how many years it would take to reach at least T dollars.
So, if the input is like P = 200, O = 10, E = 25, T = 300, then the output will be 3 as in the first year we will get interest 25%, so end up with 200+50 = 250, then next year we will get 10%, so end up with 250+25 = 275, then again get 10% in the next year, so it will be 275+27.5 = 302.5, this is greater than 300, so 3 years required.
To solve this, we will follow these steps −
- ans:= 0
- while P < T, do
- P := P * 1+(E/100)
- ans := ans + 1
- if P < T, then
- P := P * 1+(O/100)
- ans := ans + 1
- return ans
Let us see the following implementation to get better understanding −
Example
class Solution: def solve(self, P, O, E, T): ans=0 while P < T: P *= 1+(E/100) ans += 1 if P < T: P *= 1+(O/100) ans += 1 return ans ob = Solution() P = 200 O = 10 E = 25 T = 300 print(ob.solve(P,O,E,T))
Input
P = 200, O = 10, E = 25, T = 300
Output
3
