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.

Recommended Answers

All 2 Replies

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.