In: Computer Science
I'm trying to make this C++ loan calculator but I can't get the program to output what I need. For instance I know the formula is right and when I enter 100000 1.5 20 as my cin variables I should get 482.55 but I get 1543.31. What am I not seeing as the issue? Also this should cout only 2 decimal places right? I'm not allowed to alter the #include and add any fixed setprecision.
This is what I have.
#include <iostream>
#include <cmath>
using namespace std;
int main() {
// ---------------- Add code here
--------------------
// -- Declare necessary variables
here
--
int years = 0;
int LoanAmount = 0;
double AnnualRate = 0.0;
double moPayment = 0.00;
// --
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;
//rate to decimal
years = years * 12; //years to
months
moPayment = (LoanAmount * AnnualRate) / (1 - pow(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 << moPayment << endl;
return 0;
}
#include <iostream>
#include <cmath>
using namespace std;
int main() {
// ---------------- Add code here --------------------
// -- Declare necessary variables here --
int years = 0;
int LoanAmount = 0;
double AnnualRate = 0.0;
double moPayment = 0.00;
// --
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;
double MonthlyRate = AnnualRate / 1200; //get the monthly rate to decimal
years = years * 12; //years to months
// for monthly payment, monthly rate is required not Annual Rate
moPayment = (LoanAmount * MonthlyRate) / (1 - pow(1 + MonthlyRate, -years));
moPayment = round(moPayment*100)/100; // round the result to 2 decimal places
// ---------------- Add code here ------------------
// Print out the answer as a double, all by itself
// (no text) followed by a newline
// Ex. cout << payment << endl;
cout << moPayment << endl;
return 0;
}
//end of program
Output: