loan payment calculation program

Thanks for all help in advance.
My instructor wants us to create a program that relates to some event we have recently gone through and I need help to get the rest of the code to work.
Scenario I passed by the instructor is : I just got a car which my grandfather paid in advance and I have to pay him back. So I am trying to create a program to see how long I need to make monthly payments of a certain amount to pay off the debt at a certain rate of interest. I want it to where I enter the current
balance, the annual interest rate (compounded monthly), and the desired payment amount. Using a loop, at the end of each month, the balance will update. I am using the formula :

balance = balance * (1 + interestRate / 12) - monthlyPayment

to compute that change. I want the output to show the updated balance on a new line after each time it completes the loop until it reaches a zero balance or lower (negative balance). Something like

After month 1, the balance would be $xxx.xx
After month 2, the balance would be $xxx.xx

and so on until it reaches :

After month x, the balance would be $0.00



I am using Visual Studio 2008 environment. Here is what I have so far :

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
36
37
38
39
40
41
42
43
#include <iostream>
using namespace std;

int main()
{
	double monthlyPayment;
	double balance;
	double interestRate;

	cout.setf(ios::fixed);	
	cout.setf(ios::showpoint);
	cout.precision(2);

	cout << "Enter the current balance of your loan: $";
	cin >> balance;
	cout << "Enter the interest rate (compounded monthly) : ";
	cin >> interestRate;
	cout << "Enter the desired monthly payment : $";
	cin >> monthlyPayment;

	while (interestRate >= 1)
	{ 
		interestRate = interestRate / 100;   
        }
	
	balance = balance * (1 + interestRate / 12) - monthlyPayment;
		
	cout << "Your new balance is : $" << balance << endl;

        // The program stops here and doesn't complete the loop that follows.
        // This is where I'm stuck
	
        while (balance >0)

	{
		balance = balance * (1 + interestRate / 12) - monthlyPayment;

		cout << "Your balance is now : $" << balance << endl;
	}

	cout << "You have paid off the loan at this point. Congratulations!";
	return 0;
}


And I also don't know how to get the "After month x, the balance would be $x.xx" part to work either.

Thanks again for any help. I believe that even tho this is a simple program without "bells and whistles", it could be useful for people once it works properly.
Last edited on
Paste the code between the code tage (#) so that way we can read it clearly.
You have a ; on the line where while is written remove it, otherwise it seems to be fine.
add an variable for months and increment it in the while loop so you can get the total months required
Last edited on
_

Thanks for catching the ";"'s at end of the two while statements. I must have overlooked them.


Here is the updated code:

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
36
37
38
39
40
41
42
43
44
45
#include <iostream>
using namespace std;

int main()
{
	double monthlyPayment;
	double balance;
	double interestRate;
	int month = 1;

	cout.setf(ios::fixed);	// These lines force currency format in output to 2 decimal pts
	cout.setf(ios::showpoint);
	cout.precision(2);

	cout << "Enter the current balance of your loan: $";
	cin >> balance;
	cout << "Enter the interest rate (compounded monthly) : ";
	cin >> interestRate;
	cout << "Enter the desired monthly payment : $";
	cin >> monthlyPayment;
	
	while (interestRate >= 1)
	{ 
		interestRate = interestRate / 100; 
	}

	balance = balance * (1 + interestRate / 12) - monthlyPayment;
		
        // this is where the program stops. 

	cout << "After month 1 your balance is $" << balance << endl;

	while (balance > 0)
	{
		balance = balance * (1 + interestRate / 12) - monthlyPayment;
		
		month = month++;

		cout << "After month " << month << ", your balance is : $" << balance << endl;
	}

	cout << "You have paid off the loan at this point. Congratulations!" << endl;


	return 0;


When trying to get the program to reach the 1st output of what the balance is before the loop is initiated, it stops at line 27 and does not print the output from line 31. As of right now the output is:

Enter the current balance of your loan: $xxx.xx
Enter the interest rate (compounded monthly) : xx.xx
Enter the desired monthly payment : $xx

When enter is pressed after the user inputs the desired monthly payment, the program ends and closes out. I have only changed the code in deleting the ";"'s as informed but now rather I have them in or not, the program runs as said above and will not even print out the balance after calculation.

_______________________________________________________________


However, if I "Start without debugging", the program runs correctly. Any comments as to this occurrence?

Here is an example of output run when "start without debugging" is used.

Enter the current balance of your loan: $250
Enter the interest rate (compounded monthly) : 12.9
Enter the desired monthly payment : $50
After month 1 your balance is $202.69
After month 2, your balance is : $154.87
After month 3, your balance is : $106.53
After month 4, your balance is : $57.68
After month 5, your balance is : $8.30
After month 6, your balance is : $-41.61
You have paid off the loan at this point. Congratulations!
Press any key to continue . . .


I would like to keep from showing the line containing the balance when it goes negative but I do not know how to get a Boolean "range" to work within a while statement. I have attempted and the compiler will not build it.

Thanks again for your help and in advance to anyone else who does.
_
In Visual Studio "Start without debugging" is the normal way of running a program. "Star debug" will go to debug mode and you have to manually step over each line(press F10).
Other queation:
Since we are deducting a fixed amount everymonth , there is a possibility that we could overpay in the last month and balance can go negative. So we could add an if statement in the while loop
1
2
3
4
5
6
7
8
9
10
if(balance<monthlyPayment)// then calculate this way
	{
		cout << "Last month payment is " << balance << endl;
		balance=balance-balance;

	}else //oherwise
	{
		balance = balance * (1 + (interestRate / 12)) - monthlyPayment;
	}

Last edited on
or if(balance<0)cout<<" "<<endl;
I greatly appreciate everyone's help. I have made minor changes to anilpanicker's most recent post and was able to complete the program.

I was talking with people at work about the program and they said they would find it helpful if the program was able to tell exactly how much they paid in interest over the term of the loan. In theory it sounded easy, but since there is no "counter" to tell what the final number of months the loan is outstanding (due to it varying due to loan amount and interest rate), I'm stuck yet again. I know I have asked a lot of everyone, but does anyone have an idea of how to calculate said number?

Here is the code as it stands:

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
using namespace std;

int main()
{
	double monthlyPayment;
	double balance;
	double interestRate;
	double interestPaid;
	double initialBalance;
	double termOfLoan;
	double month = 1;

	cout.setf(ios::fixed);	// These lines force currency format in output to 2 decimal pts
	cout.setf(ios::showpoint);
	cout.precision(2);

	cout << "Enter the current balance of your loan: $";
	cin >> balance;
	cout << "Enter the interest rate (compounded monthly) : ";
	cin >> interestRate;
	cout << "Enter the desired monthly payment : $";
	cin >> monthlyPayment;
	
	initialBalance = balance;

	while (interestRate >= 1)       /*Converts the interest rate to a decimal if the user inputs 
                                                      in percentage form*/
	{ 
		interestRate = interestRate / 100; 
	}

	balance = balance * (1 + interestRate / 12) - monthlyPayment;
		
	cout << "After month 1 your balance is $" << balance << endl;

	while (balance > 0)
	{
		if (balance < monthlyPayment)
		{
			balance = balance - balance;
		}	
		else 
		{
			balance = balance * (1 + interestRate / 12) - monthlyPayment;
		}

		month = month++;		

		cout << "After month " << month << ", your balance is : $" << balance << endl;
		}
		cout << "You have paid off the loan at this point. Congratulations!" << endl;
		
		
		termOfLoan = month;
		
interestPaid = (monthlyPayment * termOfLoan) - initialBalance; 		

/*I believe the formula above would work if only there was
 a way to calculate how many months it took to pay 
off the loan, but since it varies, I don't know how to 
calculate "termOfLoan". Any suggestions?*/
 
        	cout << "You paid a total ammount of $" << interestPaid << " in intrest." << endl;
	        // what I would like the last line of out put to be.

	return 0;
}


Thanks again!
Hello ryujin89,

I have made a (?similar) if not same program with the same idea as yours. It uses your idea but mainly my style + etc.
It can calculate the amount of interest paid over the months but it's kind of a long code so I think it's better if you saw it all instead of a chunk only.

Can you PM me (and enable PMing) if you want to see it?
I just noticed on line 48
1
2
3
4

//month = month++;
month++; // is correct, this means month=month+1
// Also month should be an int not a double. 


On your other questions, you already have everything,
month gives you total months required to pay back loan
Then I after the while loop you calculated the total interest paid.
What else you want?
if you want to show each month principal amount and interest then you can modify the following way
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// declare two variables
double monthlyInterest=0.0;
double totalInterest=0.0
// then modify the places you used the formula
// basicallly split the calculation
//balance = balance * (1 + interestRate / 12) - monthlyPayment;
monthlyIntereset= (balance*interestRate)12;
balance=balance+monthlyIntereset;
balance=balance-monthlyPayment;
totalInterest=totalInterest+monthlyIntereset;
month++;
cout << "After month " << month << ", your balance is : $" << balance << endl;
cout << "This month you paid $" <<monthlyIntereset << "as interest and $" << monthlyPayment-monthlyIntereset << "as principal amount"<<endl;
// if you use this then you don't have to calculate the total interest later.

Hope this helps
Okay I see you want to show remaining months in every months. That is not possible before computing it right. You need compute it before showing the amortization statement. Create a function which has same foumula, something like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int monthsToPayLoan(double loan_amount, double interest, double monthpay)
{
int month=0;
double bal=loan_amount;
while (bal> 0)
	{
		if (bal< monthpay)
		{
			bal= bal - bal;
		}	
		else 
		{
			bal= bal * (1 + interest / 12) - monthpay;
		}

	month++;
}
return month;

}


Call this function before you start showing the staments and get the total months required;
 
int total_months=monthsToPayLoan(initialBalance, monthlyInterest, monthlyPayment);
I noticed the month not being int while working on program after posting it. I don't know why I changed it. in the first place, but its declared as an int now.

I do not necessarily need to show the amount of interest paid each month, just the total of interest that was paid by the end of the loan. As in, calculate but dont show what each months interest payment was and then at the end of the loan, sum up the total amount paid in interest and print it out. Hope that makes sense.

I attempted to use the code with the breakdown of the calculations you provided, but it caused an infinite loop.

I somewhat understand, but don't fully follow the breakdown and follow the math to be able to make minor changes that may or may not be needed. It makes sense when I read it (for the most part), but when I put it into the program it doesn't work.


I don't need to show the remaining months of the loan, but maybe that function could provide a way to formulate out the total interest paid at the end of the loan. Except that each month the interest amount varies so maybe not.
Last edited on
I had taken the up-to-date code that I had working and posted it in a new thread to "bump" it up and see if I could get someone to assist within the time frame I had available.


I had the core program created and was trying to get an extra function added in that would calculate the interest paid after each month and then print out the sum of those amounts at the end of the program once the loan was paid off. I described it better in this thread : http://www.cplusplus.com/forum/general/15111/ that was the one I needed, but it was due last night. I was able to get the core program which is all that was required. the other part was just something extra I wanted to add in based on what co-workers said they would like for the program to do if they had needed to use it.

I appreciate your help. Will possibly be posting other program assistance threads over the semester. Hope no one gets annoyed too much, tho it does give something to do for the retired programmers that get bored with retirement.

Thanks again
Topic archived. No new replies allowed.