simple while loop help

I need to create a code that will output a loan schedule based on these three inputs from the user:

For example..

Please enter the balance on your loan: $1000.00
Please enter the annual interest rate: 9
Please enter your monthly payment: $90.00

And then using a while loop, it will print out the following:

Payment Balance Interest Principle
1 917.50 7.50 82.50
2 834.38 6.88 83.12
3 750.64 6.26 83.74
4 666.27 5.63 84.37
5 581.27 5.00 85.00
6 495.63 4.36 85.64
7 409.34 3.72 86.28
8 322.41 3.07 86.93
9 234.83 2.42 87.58
10 146.59 1.76 88.24
11 57.69 1.10 88.90

Final Payment of: 58.12

With a payment of $90.00, it will take you
12 months to pay off your loan of $1000.00.
You will pay $48.12 in interest.

I know how to do the first part of outputting the questions and inputting the variables but how would I build a while loop to do this? and How do I build it so it will print out in the structure above (like an excel chart).

Thank you for the help

Last edited on
I might be wrong here but I think your math might be off.
If you have a 1000.00 dollar loan and make a first payment of 90.00 then you still owe 910.00.

If a 9% interest rate was added that would be 81.90 dollars added making the new remaining balance of 991.90.

So I am not sure how to follow the output you want.
@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.
Last edited on
Topic archived. No new replies allowed.