开发者

C++ Using “if else” and variable count for loop iterations need to result in zero balance

I am using the “if else” and variable count for loop iterations which needs to result in zero balance. I have written my code for the following problem but, I cannot get the final results. Which is the final balance should show a zero balance. I would like to know how to fix it. Please see problem and code below. Thank You in Advance.

PROBLEM You have just purchased a stereo system that costs $2,000 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 $75.

The monthly payment of $75 is used to pay the interest and whatever is left over is used to pay part of the remaining debt. Hence, the first month you pay 1.5% of $2,000 in interest . That’s $30 in interest. So, the remaining $45 is deducted from your debt, which leaves you with a debt of $1955.00. The next month you pay interest of 1.5% of $1955.00, which is $29.32 and you deduct $75 - $29.32 which is $45.67 from the amount you owe.

Have your program print out the month, the amount of interest paid, and the amount of the debt that is paid and the debt that remains in a nice table form. Make sure to include a line in the table for the final month that you pay. Your remaining debt should be zero for that line! Be sure to print money out to 2 decimal places, as shown below.

Sample output: 
Thank you for purchasing your new stereo system.
The following is your payment plan for the cost of $2000.00
with 1.50% interest and payments of $75.00 a month.

Month   Interest Paid   Debt Paid       Total Payment   Balance
1            30.00              45.00              75.00                 1955.00
2            29.32              45.67              75.00                 1909.33
3            28.64              46.36              75.00                 1862.96
4            27.94              47.06              75.00                 1815.91
5            27.24              47.76              75.00                 1768.15
6            26.52              48.48              75.00                 1719.67
7            25.80              49.20              75.00                 1670.47
8            25.06              49.94              75.00                 1620.52
9            24.31              50.69              75.00                 1569.83
10          23.55              51.45              75.00                 1518.38
11          22.78              52.22              75.00                 1466.15
12          21.99              53.01              75.00                 1413.15
13          21.20              53.80              75.00                 1359.34
14          20.39              54.61              75.00                 1304.73
15          19.57              55.43              75.00                 1249.30
16          18.74              56.26              75.00                 1193.04
17          17.90              57.10              75.00                 1135.94
18          17.04              57.96              75.00                 1077.98
19          16.17              58.83              75.00                 1019.15
20          15.29              59.71              75.00                 959.43
21          14.39              60.61              75.00                 898.83
22          13.48              61.52              75.00                 837.31
23          12.56              62.44              75.00                 774.87
24          11.62              63.38              75.00                 711.49
25          10.67              64.33              75.00                 647.16
26          9.71                65.29              75.00                 581.87
27          8.73                66.27              75.00                 515.60
28          7.73                67.27              75.00                 448.33
29          6.73                68.27              75.00                 380.06
30          5.70                69.30              75.00                 310.76
31          4.66                70.34              75.00                 240.42
32          3.61                71.39              75.00                 169.03
33          2.54                72.46              75.00                 96.56
34          1.45                73.55              75.00                 23.01
35          0.35                23.01              23.36                 0.00

Note that the last payment is $23.36,

Hints: Use a variable to count the number of loop iterations and hence the number of months until the debt is zero.

Be careful, the last payment may be less than $75. Also, don’t forget the interest on the last payment. If you owe $75,then your monthly payment of $75 will not pay it off, but will come close.

Make sure you look carefully at your final output to check that it is correct!

MY CODE

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

int main()
{
    float balance;
    float interest;
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);

    double loan, monthlypay, IntRat;
    double e = 0;
    double a, b, c, d;
    double Interestpay;
    double i;

    cout << "Enter the amount of the loan: $";
    cin >> loan;
    cout << "Enter the interst per year:";
    cin >> Interestpay;
    cout << "Enter the monthly pay: $";
    cin >> monthlypay;

    c = loan;

    cout << endl;
    cout << endl;
    cout << endl;
    for ( i=1; c>0; i++)


    {
        IntRat = Interestpay/100/12;

        a = IntRat*c;

        b = monthlypay-a;

        cout << "Month: "<<i<<endl;
        cout << "Principle Interest:" <<a<<endl;
        cout << "Principle Remaining: $" <<b<<endl;

        c = c-b;
        cout << "You still have a balance of: $" <<c<<endl;
        cout << endl;
        d=e+a;
        e=d;

    }

    i=i-1;
    if (c >=b)
           c = c -b;

    else if (c>0.1)
           c=c*1;
           c = 0;


        cout << "Your last payment is " <<  c << endl;

        cout <&开发者_Python百科lt; "\nThe total month is:" <<i<<endl;
        cout << "The total Interest paid is:" <<d<<endl;
        cout << "You have a credit of:" <<c<<endl;

    return 0;

}


It is generally not recommended to use floating-point types to represent currency amounts. It's typically better to use "fixed point". A simple way of doing that is to e.g. use an integer type such as int, and just scale up.

Instead of doing

double monthlyPayment = 75.0;

use

int montlyPayment = 7500;

Then you need to divide by 102 if you e.g. multiply two currency amounts together, and so on, but doing it this way can often make the calculations more exact (unless you go out of range, but for this simple example I think you'll be safe).

If you need more precision, scale by a larger amount such as 10000, but be aware that doing so decreases the range.


From a fast look, maybe you mean :

i=i-1;
if (c >=b)
{
       c = c -b;
}else if (c>0.1)
{
       c=c*1;
       c = 0;
}

In your code c = 0; is executed irrespectively of the if clause. If that is not the case please fix your code indent.

In case you mean what I post in this answer, you realize that c will be 0 ? In the case your code is what you meant, you realize that no matter what c will be 0 ?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜