Question

Once again we will create a program that tells us how many months we need to save to reach a goal. This time however, using recursive functions. The guidelines are the following:

 In main, ask the user for the initial amount, the goal and the “monthly amount” (the constant amount of money that will be put aside every month).

 Once you have these values you should call your function. Your function should not return anything and it should accept 4 arguments. These four arguments are: the goal to reach, the current amount of money, the monthly amount that will be put aside, and the number of months passed. What do you think should be the value of months passed the first time you call the function?

 As discussed in class, your recursive function should have an if and an else. Depending on how you set up your function, one of these two will be the one that returns and ends the recursion. The other one will be the one that performs the operations and re-call the function.

 When you end the recursion make sure to display the number of months needed to reach such goal (this should go right before the return statement).

Use C code to solve this problem.

Output sample:

Which amount are you trying to reach? 1000 How much money do you currently have? 100 How much will you put aside every month?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

I solved this problem in C,see below

code.c


#include <stdio.h>

void calculate(int reach_money,int cur_money,int montly_money,int no_of_months)
{
if(cur_money>=reach_money) // if cur_money >= reach_money is base condition
{
printf("The number of months you'll need is : %d",no_of_months); // print no_of_months
return; //return
}
else
{
cur_money+=montly_money; // cur_money is incremented by montly_money

no_of_months+=1; // no_of_months is incremented by 1

return calculate(reach_money,cur_money,montly_money,no_of_months); // return calculate recursively
}
}

int main()
{
int reach_money,cur_money,montly_money,no_of_months=0; // initiallly no_of_months is 0
  
printf("Which amount you are trying to reach? ");
scanf("%d",&reach_money); // taking reach_money
  
printf("How much money do you currently have? ");
scanf("%d",&cur_money); // taking cur_money
  
printf("How much will you put aside every month? ");
scanf("%d",&montly_money); // taking montly_money
  
calculate(reach_money,cur_money,montly_money,no_of_months);//calling calculate() function

return 0;
}

Code Images:-

6 نها 1 2 #include <stdio.h> 3 4 void calculate(int reach_money, int cur_money, int montly_money, int no_of_months) 5-{ if(cu{ 20 21 int main() 22 23 int reach_money, cur_money,montly_money, no_of_months=0; // initiallly no_of_months is a 24 25 print

Output Images:

Which amount you are trying to reach? 1000 How much money do you currently have? 100 How much will you put aside every month?

Which amount you are trying to reach? 200 How much money do you currently have? 0 How much will you put aside every month? 1

Thank you.! If you have any doubt please comment

Please do upvote.!

Add a comment
Know the answer?
Add Answer to:
Once again we will create a program that tells us how many months we need to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • . Programing Just with #include <stdio.h> Once again we will create a program that tells us...

    . Programing Just with #include <stdio.h> Once again we will create a program that tells us how many months we need to save to reach a goal. This time however, using recursive functions. The guidelines are the following: In main, ask the user for the initial amount, the goal and the “monthly amount" (the constant amount of money that will be put aside every month). Once you have these values you should call your function. Your function should not return...

  • Investment Recursion An investment company guarantees a 2% monthly compounded return on your inve...

    In Visual Studio Visual studio C++ Investment Recursion An investment company guarantees a 2% monthly compounded return on your investment. You want to see how long it takes to reach a total of S500.000 dollars in investment plus return. Write a program that allows the user to enter a continuous monthly investment (Example $200 every month) or ($600 every The program should call a recursive method that returns and displays the total months needed to reach the $500,000 dollar goal....

  • Write a python program (recursive.py) that contains a main() function. The main() should test the following...

    Write a python program (recursive.py) that contains a main() function. The main() should test the following functions by calling each one with several different values. Here are the functions: 1) rprint - Recursive Printing: Design a recursive function that accepts an integer argument, n, and prints the numbers 1 up through n. 2) rmult - Recursive Multiplication: Design a recursive function that accepts two arguments into the parameters x and y. The function should return the value of x times...

  • C++ PROGRAM ONLY! For this lab you need to write a program that will read in...

    C++ PROGRAM ONLY! For this lab you need to write a program that will read in two values from a user and output the greatest common divisor (using a recursive implementation of the Euclidean algorithm) to a file. In Lab #3, you implemented this program using an iterative method. Greatest Common Divisor In mathematics, the greatest common divisor (GCD) of two or more integers (when at least one of of them is zero then the larger value is the GCD....

  • You have determined that you will need SEK 3,000,000 when you retire in 40 years. You...

    You have determined that you will need SEK 3,000,000 when you retire in 40 years. You plan to set aside a series of payments each year in an account yielding 5.0 % per year to reach this goal. You will put the first payment in the account one year from today, and the payments will grow with your income by 2.5% per year. a.) Calculate your first annual payment into this account. The first annual payment is kr. (round to...

  • you start saving monthly for your child in order to accumulate 10,000 dollars once the child...

    you start saving monthly for your child in order to accumulate 10,000 dollars once the child is 18 (imagine, the child was just born). How much you must put aside every month, if yearly interest rate is 3%?

  • C - Language Create a recursive function to print a multi-digit number vertically. For example, 2378...

    C - Language Create a recursive function to print a multi-digit number vertically. For example, 2378 should be printed as 2 3 7 8 Be sure to test your program with numbers of different length. The recursive function should return an int and take an int as a parameter. The function should have a base case where the parameter is 0 and this should return 0 Define a temp variable using the remainder operator where temp is equal to the...

  • Excel is allowed! For this lab, we will create a spreadsheet that allows somebody to type...

    Excel is allowed! For this lab, we will create a spreadsheet that allows somebody to type in a loan amount, interest rate, and length of the loan in years. The spreadsheet will then calculate the monthly payment required and the actual amount paid on the loan. First, setup your spreadsheet: • In Cell A1, put the label Loan Amount:. The corresponding value would be input in Cell B1. • In Cell A2, put the label Interest Rate:. The corresponding value...

  • How to solve this problem in Excel Sheet? Assume that you are now 35 years old....

    How to solve this problem in Excel Sheet? Assume that you are now 35 years old. You would like to retire at age 65 and have a retirement fund of $5,000,000 at the time of your retirement. You have already $100,000 in the retirement account. You expect to earn 6% per year. The amount of money you must set aside each month beginning one month from now to reach your retirement goal is:

  • How many months do you need to work to have enough money to retire? Assume you...

    How many months do you need to work to have enough money to retire? Assume you want $1,000,000 to retire, you currently do not have any savings, but starting next month, you will be able to make monthly deposits of $500 into your retirement account that returns on average 5.5% per year compounded monthly. Question 18 options: 507 months 88 months 1056 months 782 months

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT