I have to make a program that uses Periodic Payments and Unpaid Balance (When Payments are made). I am having trouble with the mathematics with my code (I know this isn't a math forum, but I think it's more of a coding issue). The formulas, and output are below.....do you see any issues with the math?
Periodic Payments:
R= (Li)/(1-(1+i)^(-mt)
i = (r/m) *** r is in decimal***
Unpaid Balance (UB in my functions)
UB = R * (1-(1+i)^-(mt-k))/i
Note that if payments are monthly, then m=12.
//Intro to Programming
//PAge 342-343, Assignment #9
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
double periodic(double L, double r, double m, double t, double k, double i);
double unpaidbalance(double L, double r, double m, double t, double k, double i, double R);
int main()
{
while (true)
{
double L, r, m, t, k, i, R, r2;
cout << fixed << showpoint;
cout << setprecision(2);
cout << "Please Enter the Loan Amount : $ ";
cin >> L;
cout << "Please Enter the Interest Rate: ";
cin >> r;
cout << "Please Enter the Total Number of Payments: ";
cin >> m;
cout << "Please Enter the Length of the Loan (in Years): ";
cin >> t;
cout << "If You have made any month's payments, Enter the Number: ";
cin >> k;
cout << endl;
r2 = r / 100;
i = r2 / m;
R = periodic(L,r,m,t,k,i);
if (k>0)
{
cout << "Your Periodic Payment would be: $ " << periodic(L,r,m,t,k,i) << endl;
cout << "Since You have made " << k << " payments, your remaining balance is: $"
<< unpaidbalance(L,r,m,t,k,i,R) << endl;
cout << "\n" << endl;
}
else
cout << "Your Periodic Payment would be: $ " << periodic(L,r,m,t,k,i) << "\n";
cout << endl;
}
cout << endl;
} // end of main
double periodic(double L, double r, double m, double t, double k, double i)
{
double R, Y1, Y2, Y3;
Y1 = L * i;
Y2 = (1 + i);
Y3 = (-1)*(m*t);
R = Y1 / (1 - pow(Y2,Y3));
return R;
}
double unpaidbalance(double L, double r, double m, double t, double k, double i, double R)
{
double UB, Months, X1, X2;
Months = 12;
X1 = (1 + i);
X2 = (-1) * ((Months * t) - k);
UB = R * ((1 - pow(X1,X2)) / i);
return UB;
}
This an example of my output:
Please Enter the Loan Amount : $ 1000
Please Enter the Interest Rate: 10.00
Please Enter the Total Number of Payments: 48
Please Enter the Length of the Loan (in Years): 4
If You have made any month's payments, Enter the Number: 12
Your Periodic Payment would be: $ 6.32
Since You have made 12.00 payments, your remaining balance is: $219.14
I think the Periodic Payment is coming out to high, which is making my unpaid balance funky. Any ideas?