我正在使用"if else"和变量计数来进行循环迭代,这需要导致零余额.我已经为以下问题编写了代码但是,我无法得到最终结果.哪个是最终余额应显示零余额.我想知道如何解决它.请查看下面的问题和代码.先感谢您.
问题您刚购买了以下信用计划成本为2,000美元的立体声系统:没有首付,每年18%的利率(因此每月1.5%)和每月75美元的付款.
每月支付75美元用于支付利息,剩下的任何款项用于支付部分剩余债务.因此,第一个月您支付的利息为2,000美元的1.5%.那是30美元的利息.因此,剩余的45美元将从您的债务中扣除,这使您的债务为1955.00美元.下个月您支付的利息为1955.00美元的1.5%,即29.32美元,您从您欠的金额中扣除75美元 - 29.32美元,即45.67美元.
让您的程序打印出月份,支付的利息金额,支付的债务金额以及保留在一个漂亮的表格中的债务.确保在表格中包含您支付的最后一个月的一行.那条线的剩余债务应为零!请务必将钱打印到小数点后两位,如下所示.
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
Run Code Online (Sandbox Code Playgroud)
请注意,最后一笔付款是23.36美元,
提示:使用变量来计算循环迭代次数,从而计算债务为零之前的月数.
小心,最后一笔付款可能低于75美元.此外,不要忘记上次付款的利息.如果你欠75美元,那么你每月支付的75美元将不会支付,但会接近.
确保仔细查看最终输出,以确定它是否正确!
我的代码
#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 << "\nThe total month is:" <<i<<endl;
cout << "The total Interest paid is:" <<d<<endl;
cout << "You have a credit of:" <<c<<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
通常不建议使用浮点类型来表示货币金额.使用"固定点"通常更好.这样做的一种简单方法是使用整数类型,例如int
,并且只是按比例放大.
而不是做
double monthlyPayment = 75.0;
Run Code Online (Sandbox Code Playgroud)
使用
int montlyPayment = 7500;
Run Code Online (Sandbox Code Playgroud)
然后你需要除以10 2,如果你将两个货币数量相加,依此类推,但这样做通常可以使计算更精确(除非你超出范围,但对于这个简单的例子,我认为你'会安全的.)
如果您需要更高的精度,请按比例缩放10000
,但要注意这样做会缩小范围.