-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDepositProfit.java
30 lines (23 loc) · 908 Bytes
/
DepositProfit.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/*
You have deposited a specific amount of dollars into your bank account. Each year your balance increases at the same growth rate. Find out how long it would take for your balance to pass a specific threshold with the assumption that you don't make any additional deposits.
Example
For deposit = 100, rate = 20 and threshold = 170, the output should be
depositProfit(deposit, rate, threshold) = 3.
Each year the amount of money on your account increases by 20%. It means that throughout the years your balance would be:
year 0: 100;
year 1: 120;
year 2: 144;
year 3: 172,8.
Thus, it will take 3 years for your balance to pass the threshold, which is the answer.
*/
int depositProfit(int deposit, int rate, int threshold) {
int res=0;
double d=deposit;
double interest;
while(d<threshold){
interest=(d/100.0)*rate;
d=d+interest;
res++;
}
return res;
}