I am new to C++ programming and I'm having a little trouble.

I have to create a program that lets the user enter the loan amount and loan period in number of years and displays the monthly and total payments for each interest rate starting from 5% to 8%, with an increment of 1/8.

Loan Amount: 10000
Number of Years: 5

Interest Rate Monthly Payment Total Payment
5% 188.71 11322.74
5.125% 189.28 11357.13
5.25% 189.85 11391.59
...
7.85% 202.16 12129.97
8.0% 202.76 12165.83

The code displays the answer for 5% but I can't seem to figure out how to get it to add the increment to the interest rate and display all of the results.

Here is what I have:

``````#include "stdafx.h"

#include <iostream>
#include <cmath>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
double loanAmount;
int numberOfYears;
double annualInterestRate = 5;
double monthlyInterestRate;
double monthlyPayment;
double totalPayment;

// Enter loan amount
cout << "Loan Amount: ";
cin >> loanAmount;

// Enter number of years
cout << "Number of Years: ";
cin >> numberOfYears;

// Obtain monthly interest rate
monthlyInterestRate = annualInterestRate / 1200;

for (double i = 5; i <= 8; i += .125) {

// Calculate payment
monthlyPayment = loanAmount * monthlyInterestRate / (1
- 1 / pow(1 + monthlyInterestRate, numberOfYears * 12));
totalPayment = monthlyPayment * numberOfYears * 12;

// Format to keep two digits after the decimal point
monthlyPayment = static_cast<int>(monthlyPayment * 100) / 100.0;
totalPayment = static_cast<int>(totalPayment * 100) / 100.0;

// Display results
cout << "Interest Rate " << annualInterestRate << "% " << "\tMonthly Payment " << monthlyPayment << "\nTotal Payment " << totalPayment << endl;

return 0;
}

}``````

I can really use help. Thanks.

2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by pgr_DW808

You never use the i from your for loop in the calculation. You should use that value to calculate the monthlyInterestRate inside of the loop.

Thanks

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.