- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# 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 Questions & Answers
- 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
- 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
- Python Program to find out how many times the balls will collide in a circular tube
- Program to find how many lines intersect in Python
- Program to count how many swimmers will win the final match in Python
- Program to find the formatted amount of cents of given amount in Python
- How long does it take to learn Python?
- Program to find minimum jumps to reach home in Python
- Program to find hoe many children will get candies while distributing them maintaining the rules in Python
- C++ program to calculate how many years needed to get X rupees with 1% interest
- Python Program to find out how many cubes are cut

Advertisements