/** * payment.cpp * Anthony Larrain * Spring 2006, csc309 * * This program will compute the monthly payment on a loan, given the * loan amount, rate in years and number of years of the loan */ #include // needed for the pow function #include #include using namespace std; void getInput(double& principal, double& rate, int& years); double computePayment(double principal, double rate, int years); void showPayment(double principal,double rate, int years); const int MONTHS_IN_YEAR = 12; const int PERCENT_TO_DECIMAL = 100; int main(int argc, char *argv[]) { double principal,rate; int years; getInput(principal,rate, years); showPayment(principal,rate, years); system("PAUSE"); return EXIT_SUCCESS; } void getInput(double& principal,double& rate, int& years){ cout <<"Enter the amount you would like to borrow" << endl; cin >> principal; cout <<"Enter the rate of the loan" << endl; cin >> rate; cout <<"Enter the number of years" << endl; cin >> years; return; } double computePayment(double principal, double rate, int years){ double monthlyRate = rate / PERCENT_TO_DECIMAL / MONTHS_IN_YEAR; double numerator = monthlyRate * pow(monthlyRate + 1, MONTHS_IN_YEAR * years); double denominator = pow(monthlyRate + 1, MONTHS_IN_YEAR * years) - 1; return principal * numerator / denominator; } void showPayment(double principal,double rate, int years){ cout <<"Your monthly payment is " << fixed << setprecision(2) <<"$" << computePayment(principal,rate,years) << endl; }