Question

In: Computer Science

Write a function howManyMonths(start, rate, spending, target) where: start - the starting balance in your bank...

Write a function howManyMonths(start, rate, spending, target) where:

start - the starting balance in your bank account (float)

rate - the monthly interest rate (float) - like 0.001 for 0.1%

spending - the monthly amount that is spent each month (float)

target - the target savings that you’d like to achieve

The function returns how many months it takes to save the target savings amount. The function should return a -1 if the balance goes below 0 because the spending is too high. The function should also return -1 if the target is not reached after 100 months.

Each month you earn the monthly rate of interest, but you also spend the spending amount.

So, next_month_balance = start_balance * (1 + rate) - spending

Solutions

Expert Solution

since the language is not mentioned c++ is use here

  • the function accepts all thee given parameters
  • we use a while loop for the main calculation
  • a variable named no_ofmonths count the number of months
  • the while loop executes until the target equals the start which is the current balance
  • function also terminates when balance becomes less than 0 or no_ofmonths exceeds 100 by returning a -1 value
  • each time the loop executes the current balance start value is re calculated using ,next_month_balance = start_balance * (1 + rate) - spending

code in python

def  HowManyMonths(start,rate,spending,target):
    no_ofmonths=0
    while start<target:
        no_ofmonths=no_ofmonths+1
        if(no_ofmonths>=100):#checking months exceeded 100
            return -1
        start = start* (1 + rate) - spending
        if(start<0):#checking balance less than 0
            return -1
    return no_ofmonths
print(HowManyMonths(1000,.3,200,2000))

code in c++

​

#include <iostream>

using namespace std;
int HowManyMonths(float start,float rate,float spending,float target)
{
    int no_ofmonths=0;
    
    while(start<target)
        {    //cout<<start<<"  ";// change this to see updated start value after each iteration
            no_ofmonths=no_ofmonths+1;
            if(no_ofmonths>=100)//checking if no of months exceeds 100
            {
                return -1;
            }
            start = start * (1 + rate) - spending;
            if(start<0) //checking if balance if under 0
            {return-1;}
            
        }
        
        return no_ofmonths; 
        
    

}

int main()
{
    cout<<"\n"<<HowManyMonths(1000,.3,200,2000);

    return 0;
}

​

output

the output of the above code will be :

6


Related Solutions

Your bank offers to lend you $114,400 at an 8.5% annual interest rate to start your...
Your bank offers to lend you $114,400 at an 8.5% annual interest rate to start your new business. The terms require you to amortize the loan with 10 equal end-of-year payments. How much interest would you be paying in Year 2? a. $10,428.81 b. $8,615.10 c. $9,068.53 d. $7,254.82 e. $8,161.67
Suppose the overnight market rate is below the target rate set by the Bank of Canada...
Suppose the overnight market rate is below the target rate set by the Bank of Canada (BoC). To reduce the downward pressure on overnight rate, the Bank of Canada decides to use open market operations with a target volume of 100 million dollars. (a) Describe the actions BoC can undertake to intervene in the overnight market. (b) Using T-accounts, record the changes in balance sheets of BoC and the financial system following BoC’s intervention.
Suppose the current inflation rate is higher that the target inflation rate. Would the Central bank...
Suppose the current inflation rate is higher that the target inflation rate. Would the Central bank increase or decrease the interest rate? In your answer, explain how the Central bank makes this decision and explain the steps involved in changing the interest rate.
Suppose the current inflation rate is higher that the target inflation rate. Would the Central bank...
Suppose the current inflation rate is higher that the target inflation rate. Would the Central bank increase or decrease the interest rate? In your answer, explain how the Central bank makes this decision and explain the steps involved in changing the interest rate.
"sum_between" function Write a function named "sum_between" that receives 2 parameters - "start" (an int) and...
"sum_between" function Write a function named "sum_between" that receives 2 parameters - "start" (an int) and "end" (an int). It should return the sum (total) of all of the integers between (and including) "start" and "end". If "end" is less than "start", the function should return -1 instead. e.g. if you give the function a start of 10 and an end of 15, it should return 75 (i.e. 10+11+12+13+14+15)
RACKET a) Write a recursive function (gen-list start end). This function will generate a list of...
RACKET a) Write a recursive function (gen-list start end). This function will generate a list of consecutive integers, from start to end. If start > end then an empty list is generated. For example: (gen-list 1 5) ---> (1 2 3 4 5) b) write a recursive function pair-sum? that takes an integer sequence as generated by the gen-list function in exercise 4 above. This function tests whether any two adjacent values in the given list sum to the given...
Cost function, breakeven, target profit, uncertainties and bias, interpretation   Joe Davies is thinking about starting a...
Cost function, breakeven, target profit, uncertainties and bias, interpretation   Joe Davies is thinking about starting a company to produce carved wooden clocks. He loves making the clocks. He sees it as an opportunity to be his own boss, making a living doing what he likes best. Joe paid $300 for the plans for the first clock, and he has already purchased new equipment costing $2,000 to manufacture the clocks. He estimates that it will cost $30 in materials (wood, clock...
Write a summary on chapter “Commercial Bank and its function” in your own words ONLY, (600...
Write a summary on chapter “Commercial Bank and its function” in your own words ONLY, (600 to 700 words).
Write a summary on chapter “Commercial Bank and its function” in your own words ONLY, (500...
Write a summary on chapter “Commercial Bank and its function” in your own words ONLY, (500 to 600 words) Pls Type
Write a Python function that returns a list of keys in aDict with the value target....
Write a Python function that returns a list of keys in aDict with the value target. The list of keys you return should be sorted in increasing order. The keys and values in aDict are both integers. (If aDict does not contain the value target, you should return an empty list.) This function takes in a dictionary and an integer and returns a list. def keysWithValue(aDict, target): ''' aDict: a dictionary target: an integer ''' # Your code here
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT