In: Computer Science
I need to create a monthly loan Calculator in C++. I know the formula but can't figure out what's wrong with my code. Any clarification would be appreciated!
// Only add code where indicated by the comments.
// Do not modify any other code.
#include <iostream>
#include <cmath>
using namespace std;
int main() {
// ---------------- Add code here
--------------------
// -- Declare necessary variables
here
--
int years = 0; //n
int LoanAmount = 0;
double AnnualRate = 0.0; //r
double payment = 0.0;
// --
cout << "Enter the amount, rate as a
percentage (e.g. 3.25), and number of years\n";
cout << " separated by spaces: " <<
endl;
// ---------------- Add code here
--------------------
// -- Receive input and compute the monthly payment
--
cin >> LoanAmount >> AnnualRate >>
years;
AnnualRate = AnnualRate / 100;
years = years * 12;
payment = ((LoanAmount * AnnualRate) / 1 - (1 +
AnnualRate) * -years);
// ---------------- Add code here
------------------
// Print out the answer as a double, all by
itself
// (no text) followed by a newline
// Ex. cout << payment << endl;
cout << payment << endl;
return 0;
}
Code:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int years;
int loanAmount;
double annualRate;
double payment;
// getting input
cout << "Enter the amount, rate as a percentage
(e.g. 3.25), and number of years" << endl;
cout << "separated by spaces: " <<
endl;
cin >> loanAmount >> annualRate >>
years;
annualRate = annualRate/100; // converting rate
into decimal
years = years*12; // number of months
// payment using formula
payment = (loanAmount*annualRate)/(1 -
pow(1+annualRate, -years));
// output
cout << payment << endl;
return 0;
}
OUTPUT: