while loop not working

Problem:
You have just purchased a stereo system that costs $1000 on the following credit plan: No down payment, an interest rate of 18% per year (and hence 1.5% per month), and monthly payments of $50. The monthly payment of $50 is used to pay the interest and whatever is left is used to pay part of the remaining debt. Hence, the first month you pay 1.5% of $1000 in interest. That is $15 in interest. So, the remaining $35 is deducted from your debt which leaves you with a debt of $965.00. The next month you pay interest of 1.5% of $965.00, which is $14.48. Hence, you can deduct $35.52 (which is $50 - $14.48) from the amount you owe.

Write a program that will tell you how many months it will take you to pay off the loan, as well as the total amount of interest paid over the life of the loan. Use a loop to calculate the amount of interest and the size of the debt after each month. Put out the monthly amount of interest paid and remaining debt. Use a variable to count the number of loop iterations and hence the number of months until the debt is zero. You may want to use other variables as well. The last payment may be less than $50 if the debt is small, but do not forget the interest. If you owe $50, then your monthly payment of $50 will not pay off your debt, although it will come close. One month's interest on $50 is only 75 cents.

My coding:
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
#include <iostream>

using namespace std;
int main()
{

	//Declare constant payment as double = 50
	const double payment = 50;

	//Declare variable interest rate as double
	double interestRate;
	//Declare variable months as integer
	int months = 0;
	//Declare variable balance as double = 1000
	double balance = 1000;
	//Declare variable remaining debt as double
	double remainingDebt;
	//Input interest rate
	cout << "Input interestRate" << endl;
	cin >> interestRate;
	interestRate = interestRate / 100;

	/*While loop for balance is greater than 0
	Balance = (((balance * interest rate) + balance) – payment)
	Remaining debt = 1000 – 50 (interest rate) */

	while (balance > 0) {
		balance = (((balance * interestRate) + balance) - payment);
		months++;
	}


	//Print months
	cout << months << endl;
	//Print total interest paid
	cout << totalInterestPaid << endl;


	system("pause>nul");
	return 0;
}
Hi,

Could you tell us what isnt working, what the loop should do, and what it is currently doing, would help much more than the entire assignment.
Last edited on
I think I'm confused on how to get the total interest paid. I'm not sure if I'm messing up on the math or the coding. probably both.

like I'm not sure where I should put total interest paid into the loop here:
1
2
3
4
	while (balance > 0) {
		balance = (((balance * interestRate) + balance) - payment);
		months++;
	}


(sorry I'm really new at this. I got it working yesterday but I saved it wrong and some parts of it vanished.)
Last edited on
Hey,

Since you have done a good job on the task so far, I decided to put in the effort and finish it with comments on how it works. I made it quickly so Im sorry if I missed something. I'm not 100% sure if my program covers this part -

The last payment may be less than $50 if the debt is small, but do not forget the interest. If you owe $50, then your monthly payment of $50 will not pay off your debt, although it will come close. One month's interest on $50 is only 75 cents.


Although I think I did since if I start the debt at $50, the remaining debt will be 0.75 cents.

Full program:
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
#include <iostream>

using namespace std;

int main()
{
	const int paymentPerMonth = 50; // $50 monthly payment
	const double interestRate = 0.015; // 1.5% Interest per month
	double remainingDebt = 1000.0; // Start with $1000 left to pay
	double currentInterest = 0; // Keeps track of each months interest
	int amountOfMonths = 0; // Self-explanatory
	double totalAmountOfInterestPaid = 0; // Same

	bool run = true;
	while (run) // run as long as run equals true
	{
		currentInterest = interestRate * remainingDebt; // How much interest has to be payed per month
		totalAmountOfInterestPaid = totalAmountOfInterestPaid + currentInterest; // Adding up all the interests each month
		remainingDebt = remainingDebt - (paymentPerMonth - currentInterest); // Paying off the debt, which is 50$ - this months interest
		amountOfMonths++; // increase by 1 month each iteration, since one iteration represents a month

		if (remainingDebt <= 0) // Once the debt is payed off, ends up at -$2
			run = false; // Set run to false to end the loop
	}

	cout << "Amount of months to pay of $1000 Debt: " << amountOfMonths << endl;
	cout << "Total amount of interest payed during the " << amountOfMonths << " months: " << totalAmountOfInterestPaid << endl;

	system("pause");
	return 0;
}


Cheers =)
Last edited on
thank you SO much. I spent hours so confused by this and was getting no where, so I really appreciate the comments and explanations so I can finally understand!!
Topic archived. No new replies allowed.