recursion
Recursion: solving a problem with a method that calls itself.
compound interest: algorithm
BEGIN DO Ask the user for the deposit, interest rate and number of terms WHILE all of the values are positive, and the interest rate is in decimal format Send these values to the Compounder method When the Compounder method is done, output the final amount saved END Compounder method (requires deposit, interest rate and number of terms): BEGIN Set the variable final amount saved to the deposit If the number of terms remaining equals 0 Then return the final amount saved Else call this Compounder method again, but this time send it: The (final amount saved * interest rate) + final amount saved as the new deposit, The same interest rate, And the number of terms - 1 End if END
compound interest: java
public class compoundInterest{ public static double compound(double amountInBank, double interestRate, int numberOfTerms){ if (numberOfTerms == 0) { return amountInBank; } else { return (compound(amountInBank + (amountInBank * interestRate), interestRate, numberOfTerms-1)); } } public static void main(String[] args) { //invest $100 at 5% for 2 years (compounding annually, so only 2 terms): double final_amount = compound(100, 0.05, 2); System.out.println(final_amount); //invest $100 at 5% per year *paid monthly*, for 5 years: final_amount = compound(10000, (0.05/12), (5*12)); System.out.println(final_amount); } }