
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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
- Related Articles
- Program to find how long it will take to reach messages in a network in Python
- Program to find minimum number of cells it will take to reach bottom right corner in Python
- In how many years will ruppes 4000 amount to rupee 4630.50 at 5%p.a.compounded annually
- Program to find number of days it will take to burn all trees in python
- Program to find how many total amount of rain we can catch in Python
- Program to schedule tasks to take smallest amount of time in Python
- C++ Program to find how many pawns can reach the first row
- Program to find minimum jumps to reach home in Python
- Program to find how many lines intersect in Python
- Python Program to find out how many times the balls will collide in a circular tube
- Program to find the formatted amount of cents of given amount in Python
- People from villages move to cities to seek employment opportunities. How many years will it take for 1216 people from a village to move to a city at a rate of 152 per year? Find the number of people who move to cities in 3 years.
- Program to count how many swimmers will win the final match in Python
- How long does it take to learn Python?
- Program to find number of given operations required to reach Target in Python

Advertisements