We help IT Professionals succeed at work.

# c++ help to determine mortgage balance, interest and principal

meaar asked
on
840 Views
Last Modified: 2010-04-01
Hello,

I am working on a program.  I have it asking for the mortgage amount, interest and term of the loan and that calculation appears to be correct.  I need to determine the mortgage balance , monthly interest paid and monthly principal paid.  I was given a math calculation to determine the mortgage balance, but that number appears off.  I am at a total loss on the calucation to determine the monthly interest paid and monthly principal paid.  Eventually I will want to present something similar to this for the entire loan as well as the option to only print 10 or so lines per page, but my calculations are off.

Monthly       Total Loan      Monthly         Monthly
Payment #     Balance         Interest Paid   Principal Paid
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
360           499399.24       2083.33         600.76
359           498799.21       2080.83         600.04
358           498199.90       2078.33         599.32
357           497601.31       2075.83         598.60
356           497003.44       2073.34         597.88
355           496406.28       2070.85         597.16
354           495809.84       2068.36         596.44
353           495214.12       2065.87         595.72

For example my program calculates this:

mortage amount 100,000
interset 8.75
term 30 years

Monthly payment = 786.70 (this number is correct per my real estate calculator)
Mortgage balance = 93,591.40 (this number is off, it should be 98,496.92)

If someone can help me determine what the correct math calculation should be and some help with a direction on how to produce the above output (payment #, balance, interest paid and principal paid) I would be very greatful!

Here is my code:

------------------------------------------------------------------------------------
main.cpp
------------

#include <loan.h>

#include <string>

using namespace std;

int main()
{
double AmtofLoan, IntRate;
int Years, NumberOfPay;

while (true) {

// Ask question and put the answers into variables to
// be used later.
cout << "Enter the loan amount in dollars: ";
cin >> AmtofLoan;
cout << "Enter the loan term in years: ";
cin >> Years;
cout << "Enter the interest rate as a percentage (example: 8.75): ";
cin >> IntRate;
cout << "Enter the total number of paid monthly loan payments (example: ";
cout << "12 for a years worth of payments): ";
cin >> NumberOfPay;
cout << endl;
cin.get();

// Create a loan object here and pass in the
// the values of Amount, Years, and Rate.
Loan myLoan;
myLoan.Amount(AmtofLoan);
myLoan.setTerm(Years);
myLoan.setRate(IntRate);
myLoan.Payments(NumberOfPay);

// Use the myLoan object from now on.
cout << "Loan amount of \$" << myLoan.Amount(AmtofLoan) << endl;
cout << "For a term of " << myLoan.getTerm(Years) <<" months" << endl;
cout << "With an interest rate of " << IntRate << "%" << endl;
cout << "Returns a monthly mortgage payment of \$" << myLoan.Mortgage() << " per month." << endl;
cout << "Total number of payments you have made are: " << myLoan.Payments(NumberOfPay) << endl;
cout << "The balance of your loan is \$" << myLoan.Balance() << endl;
cout << endl;
cout << "Press enter to continue...." << endl;
cin.get();

// Ask if user wants to enter new numbers or quit?
cout << "Do you want to continue? 'y' for yes, anything else to quit" << endl;
string endit;
cin >> endit;

// If user enters anything but, "y" then the program will end.
if (endit != string("y")) break;
}
return 0;
}

--------------------------------------------------------------------------------------------
loan.cpp
-------------
#include <loan.h>

using namespace std;

// Function to set the MonthRate
double Loan::setRate (double IntRate)
{
MonthRate=IntRate / (12 * 100);
return MonthRate;
}

// Function to set the MonthTerm
int Loan::setTerm (int Years)
{
MonthTerm=Years * 12;
return MonthTerm;
}

// Function to get therm MonthTerm
int Loan::getTerm(int)
{
return MonthTerm;
}

// Function to get the MonthRate
double Loan::getRate(double)
{
return MonthRate;
}

// Function to get the MonthAmount
double Loan::Amount(double AmtofLoan)
{
MonthAmount=AmtofLoan;
return MonthAmount;
}

int Loan::Payments(int NumberOfPay)
{
TotalPayments=NumberOfPay;
return TotalPayments;
}

// Function to do the math and get the MortgagePay
double Loan::Mortgage(void)
{
// M = P * ( J / (1 - (1 + J) ** -N))
MortgagePay = MonthAmount * MonthRate/ (1-(pow((1+MonthRate), -MonthTerm)));
return MortgagePay;
}

double Loan::Balance(void)
{
// P = P * (1 - ((1 + J) ** t - 1) / ((1 + J) ** N - 1))

MortBal = MonthAmount * (1 - ((1 + MonthRate) * TotalPayments - 1) / ((1 + MonthRate) * MonthTerm - 1));

return MortBal;
}

---------------------------------------------------------------------------------------------------

loan.h
------------
#include <math.h>
#include <iostream>

using namespace std;

// Create a new class called Loan
class Loan
{
public:

double MonthRate, MonthAmount, MortgagePay, MortBalance, MortBal;
int MonthTerm, TotalPayments;

double Amount(double),setRate (double), getRate(double), Mortgage(void), Balance(void);
int setTerm (int), getTerm(int), Payments(int);

};

Comment
Watch Question
CERTIFIED EXPERT
Top Expert 2012
Commented:
This problem has been solved!
Unlock 1 Answer and 1 Comment.
###### Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
###### Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

• Troubleshooting
• Research
• Professional Opinions
###### Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE