Write a simple program in c++. A savings account typically accrues savings using
ID: 3564453 • Letter: W
Question
Write a simple program in c++. 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 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
Explanation / Answer
Here is the code that is working perfecly. Please rate friend i had to spend tme to work on it.
#include<iostream>
using namespace std;
double iterativeInterest(double principle, double interest, int years){
double ans=principle;
for(int i=1;i<=years;i++){
ans+=((interest)*ans);
}
return ans;
}
double recursiveInterest(double principle, double interest, int years)
{
if (0 == years) {
return principle;
} else {
return (1+interest) * recursiveInterest(principle, interest, years - 1);
}
}
int main(){
double p,r;
int t;
cout << "Enter initial amount to save: " ;
cin >> p;
cout << "Enter yearly interest rate (e.g. 0.10 for 10%):" ;
cin >> r;
cout << "Enter number of years of compounded interest: " ;
cin >> t;
cout << "Recursive: " << p << " after " << t << " years, at " << r << "would amount to $" << recursiveInterest(p,r,t) << endl;
cout << "Iterative: " << p << " after " << t << " years, at " << r << "would amount to $" << iterativeInterest(p,r,t) << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.