@Manga, you are correct that the math is suspicious, but the 9% interest rate is applied annually, not monthly. Therefore, when making a $90 payment on $1000 worth of loan, a $910 balance left over would accrue an additional $6.825 of interest.
Here's an example, that while it may still need some work, should give you an idea of how to implement this using a while loop.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
#include <iomanip>
#include <iostream>
using namespace std;
int main()
{
int loan,interest,monthly;//ints used for inputting information
float rate,balance;//floats used to track dollar values with greater precision
float paid_interest=0;//used to tally paid interest
cout<<"Please enter the balance of the loan: ";
cin>>loan;
balance=loan;//int to float conversion
cout<<"Please enter the interest rate: ";
cin>>interest;
rate=(float)interest/12;//int to float conversion while dividing by 12 for monthly rate
rate=rate/100;//conversion of rate to a percentage
cout<<"Now, please enter your monthly payment: ";
cin>>monthly;
cout<<"\nPayment Balance Interest Principle\n";
int i=0;
while (i<12)
{ // payment #, balance, interest paid and principle paid
cout<<" "<<i+1<<"\t "<<setprecision(2)<<fixed<<balance<<"\t "<<rate*balance<<"\t ";
if (balance>monthly-(rate*balance)
cout<<monthly-(rate*balance)<<endl;
else
cout<<balance<<endl;
paid_interest+=(rate*balance);//tally interest costs
balance=balance-(monthly-(rate*balance));//subtract principle payment from loan
i++;//increment i to execute loop 12 times
}
//summarizes 12th output by listing principle+interest to pay
cout<<"The final payment is "<<balance+(monthly-(rate*balance))+rate*balance<<"."<<endl;
cout<<"You paid "<<paid_interest<<" in interest through the life of the loan.\n";
}
|
Please enter the balance of the loan: 1000
Please enter the interest rate: 9
Now, please enter your monthly payment: 90
Payment Balance Interest Principle
1 1000.00 7.50 82.50
2 917.50 6.88 83.12
3 834.38 6.26 83.74
4 750.64 5.63 84.37
5 666.27 5.00 85.00
6 581.27 4.36 85.64
7 495.63 3.72 86.28
8 409.34 3.07 86.93
9 322.41 2.42 87.58
10 234.83 1.76 88.24
11 146.59 1.10 88.90
12 57.69 0.43 57.69
Your final payment is 58.12.
You paid 48.12 in interest through the life of the loan. |
I notice that there are a few differences in your sample output, such as how you applied principle payments before listing them (look at the balance of the first payment on mine vs yours). There may some math fundamentally off in either my calculations, or yours involving interest rates and how they are applied. However, this should give you the ideas needed to play with the sequence of the commands and formulas involved to get what you want.
EDIT- Typo correction. It's also occurred to me that the math has to be wrong, because 12 $90 payments would be $1080, which would mean $80 in interest paid, but %9 APR means that $90 should be paid. I think Manga was correct that accrued interest needs to be applied to the loan balance.