The language I\'m using is C++. Implement a function double addInterest(double l
ID: 3582554 • Letter: T
Question
The language I'm using is C++. Implement a function
double addInterest(double loanAmount, double interestPerYear, int numYears)
that returns the loan amount after adding interest for numYears years. For example if loanAmount is 1000, interestPerYear is 0.1 and numYears is 2, then the after the first year loanAmount would be increased to 1000 + 1000*0.1 = 1100 and after the second year the value would be increase to 1100 + 1100*0.1 = 1210. Don’t call pow or any other cmath function. Instead, use a for loop to add the interest year by year.
Explanation / Answer
Here is function:
double addInterest(double loanAmount, double interestPerYear, int numYears)
{
//intializes the loan amount to interest
double interest = loanAmount;
for(int i=0;i<numYears;i++)
{
//calculate the interest for every year
interest = interest + interest * interestPerYear;
}
return interest;
}
Sample code to test:
#include <iostream>
double addInterest(double loanAmount, double interestPerYear, int numYears);
using namespace std;
int main()
{
cout <<addInterest(1000,0.1,2) << endl;
return 0;
}
double addInterest(double loanAmount, double interestPerYear, int numYears)
{
//intializes the loan amount to interest
double interest = loanAmount;
for(int i=0;i<numYears;i++)
{
//calculate the interest for every year
interest = interest + interest * interestPerYear;
}
return interest;
}
Output:
1210
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.