1

Im trying to write a program that uses a recursive method to calculate how many months it will take to reach a goal of 10 million total invested if the same amount of money(inputted by the user) is invested with a 2 percent interest added each month. The problem is the method is returning counter too early so my "months" output is inaccurate. My guess is that my last else statement is wrong or my counter is placed incorrectly

Heres my code

   import java.util.Scanner;
    public class MoneyMakerRecursion {
        public static int counter = 0;
        public static void main(String[] args) {
            //Variables declared
            Scanner userInput = new Scanner(System.in);
            double investment;
            //User is prompted for input
            System.out.println("Enter your monthly investment: ");
            investment = userInput.nextInt();
            //Method is called
            Sum(investment);
            //Results from recursive method output 
            System.out.println("It should take " + counter + " month(s) to reach your goal of $10,000,000");
        }
        //recursive Method
        public static double Sum(double investment) {
            counter++;
            double total = (investment * 0.02) + investment;
            if(total >= 10000000) {return counter;}
            else {return Sum(investment+total);}
        }
    }
1
  • No, the problem is that you're doubling the investment at each iteration, by adding it to the total. Commented Apr 2, 2017 at 15:07

1 Answer 1

1

Important point you missed is that your monthly investment is same throughout all months. Hence it should be static variable.

Second point you were adding investment to total which was local variable to that method. Which is not a actual investment for month. its a value passed to that function which changes for each month(Consider your code for this statement)

See below working code.

import java.util.Scanner;
    public class MoneyMakerRecursion {
        public static int counter = 0;
        public static double investment = 0;
        public static void main(String[] args) {
            //Variables declared
            Scanner userInput = new Scanner(System.in);
            //User is prompted for input
            System.out.println("Enter your monthly investment: ");
            investment = userInput.nextInt();
            //Method is called
            Sum(investment);
            //Results from recursive method output 
            System.out.println("It should take " + counter + " month(s) to reach your goal of $10,000,000");
        }
        //recursive Method
        public static double Sum(double totalInvestment) {
            counter++;
            double total = (totalInvestment* 0.02) + totalInvestment;
            if(total >= 10000000) {return counter;}
            else {return Sum(total+investment);}
        }
    }

Result

Enter your monthly investment: 
100000
It should take 55 month(s) to reach your goal of $10,000,000

Here is snapshot: here interest is considered annually hence converting 0.02 monthly interest to per year interest it becomes 0.24

enter image description here

Sign up to request clarification or add additional context in comments.

6 Comments

Nice catch, I agree with your logic +1 ... But then who downvoted the quesiton?
@TimBiegeleisen i downvoted your answer. sorry sir for not providing reason to you. Hope this answer satisfy you why i downvoted your answer
@JBNizet please go through question. you will come across this line "if the same amount of money(inputted by the user) is invested "
Your logic makes alot of sense to me. However for some reason. It still doesnt achieve the desried result. See this calculator for a reference. financialmentor(dot)com/calculator/savings-account-calculator. An input of 100k should yield 93. But instead it yields 55.
hey i will add snapshot
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.