Problem1: A savings account typically accrues savings using compound interest. I
ID: 3676323 • Letter: P
Question
Problem1:
A savings account typically accrues savings using compound interest. If you deposit $1,000 with a 10% interest rate per year then after one year you have $1100. If you leave this money in the account for another year at 10% interest then after two years you have $1,210. After three years you would have $1,331, and so on.
Write a C++ program that inputs the amount of money to save, an interest rate per year, and the number of years the money will accrue compound interest. Write BOTH a recursive and an iterative function that calculates the amount of money that will be in the savings account using the input information.
To verify your function, the amount should be equal to P(1+i)n where P is the amount initially saved, i is the interest rate per year, and n is the number of years.
Sample output (bolded text denote input from user)
Enter initial amount to save: 1000
Enter yearly interest rate (e.g. 0.10 for 10%): 0.1
Enter number of years of compounded interest: 3
Recursive: $1000.00 after 3 years, at 0.10 would amount to $1331.00
Iterative: $1000.00 after 3 years, at 0.10 would amount to $1331.00
NOTE: Your program must compile and run correctly. Make sure that you are submitting both your executable file and source code.
Explanation / Answer
import java.util.*;
public class Account savings
{
public static void main(String[]args)
{
double interestRate;
double initialDeposit;
double totalYears;
Scanner keyboard = new Scanner (System.in);
System.out.println("Please enter your initial amount deposited:");
initialDeposit=keyboard.nextDouble();
Savings.errorsCheck(initialDeposit);
System.out.println("Now enter your annual interest rate as a decimal:");
interestRate = keyboard.nextDouble();
System.out.println("Finally, enter the number of years you plan to accumulate interest:");
totalYears = keyboard.nextDouble();
double finalAmount = Savings.errorsCheck(initialDeposit, interestRate, totalYears);
System.out.println("After " + totalYears + " number of years, your balance will be: $" + finalAmount);
}
private static double finalAmount(double interestRate, double initialDeposit, double totalYears)
{
if (totalYears ==0)
{
return initialDeposit;
}
else
{
return finalAmount(initialDeposit + (initialDeposit*interestRate), interestRate, (totalYears - 1));
}
}
public static double errorsCheck(double interestRate, double initialDeposit, double totalYears)
{
if(initialDeposit < 0.0)
{
System.out.println("Error: The deposit you entered is less than zero.");
}
if(interestRate < 0.0)
{
System.out.println("Error: The interest rate you entered is less than zero.");
}
if(totalYears < 0.0)
{
System.out.println("Error: The number of years you entered is less than zero.");
}
return Savings.finalAmount(interestRate, initialDeposit, totalYears);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.