You have just purchased a stereo system that cost $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. Use a variable to count the number of loop iterations and hence the
number of months until the debt is zero. 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 to close.
One month’s interest on $50 is only 75 cents


That is the problem i have to do for my C++ class. I'm confused as to which kind of loop to use for this. We've ben taught, while, do-while, and for loops. I never really understood do-while or for loops, and I probably couldn't even do the above problem math wise let alone programming it. If someone could help me as to which kind of loop would be best used for this or point me in the right direction that would help a lot.

My rule of thumb is to use for loops if I have control over when the loop will stop---for example if I want it to work X times or if I want it loop while X < Y, etc, AND if the increment I want to make is the same each time through the loop. If those criteria can't be met then I use a while or do/while loop. I usually use a while loop unless I want the while loop to run at least once, no matter what, in which case I use a do/while loop.

okay i am entirely confused on how to even do this problem.

I try things for the sake of trying them, making sure i understand the for loop, but it always loops infintely.

I don't get it when our teacher guves us an example like this:

j = 2;
for (int i = 0; i <= 5; i++)
{
cout << j << “ ”;
j = 2 * j + 3;
}
cout << j << “ ” << endl;

That was one of the written questions i had to answer for our homework, and I only know the answer because I cheated and stuck in a program and ran it. I don't understand why there is an i variable in there.

That loop is saying that you are going to start the loop with i initialized to 0, and increment i by 1 for each iteration. You will keep looping as long as i is less than 5.
So, the first time through the loop i will be 0, then 1, then 2, then 3, then 4.
Then, i will be set to 5, and the condition "i <= 5" will fail so the loop will be exited. So, you are doing j = 2*j + 3; five times.

Okay I couldn't get it to work as a for loop so i used a while.

#include <iostream>
#include <iomanip>
using namespace std;

int main ()
{
    // declaring vairables //
    double debt = 1000;
    double interest;
    double pay;
    double tinterest = 0;
    int month = 0;

    cout << "You have bought a stereo that costs $1000 with the following payment plan:" << endl;
    cout << "$0 down payment, 18% annual interest rate, and monthly payments of $50." << endl;
    // loop to determine how many months to pay off debt, and the total interest paid //
    while (debt>0)
    {
        interest = debt * .015;
        pay = 50 - interest;
        debt = debt - pay;
        month = month + 1;
        tinterest = tinterest + interest;
    }

    cout << "It will take you " << month << " months to pay off your debt." << endl;
    cout << "The total amount of interest paid is $" << fixed << setprecision(2) << tinterest << endl;
}

I think it works right.

That loop is saying that you are going to start the loop with i initialized to 0, and increment i by 1 for each iteration. You will keep looping as long as i is less than 5.
So, the first time through the loop i will be 0, then 1, then 2, then 3, then 4.
Then, i will be set to 5, and the condition "i <= 5" will fail so the loop will be exited. So, you are doing j = 2*j + 3; five times.

I think you mean when i is set to 6 it will fail, thus 5 iterations.

In relation to the original question, I would use a while loop with the test condition checking against the ever decreasing balance owed. Each iteration equating to a month.

I think you mean when i is set to 6 it will fail, thus 5 iterations.

Oops, lol. I did mean 6, but that would make it 6 iterations.

Oops, lol. I did mean 6, but that would make it 6 iterations.

Oops straight back at ya.......lol

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.