C++ 以C+表示的贷款偿还计算+;
我正在写一个程序,它将计算每月的贷款付款。但它并没有给出正确的答案。这是我的密码:C++ 以C+表示的贷款偿还计算+;,c++,C++,我正在写一个程序,它将计算每月的贷款付款。但它并没有给出正确的答案。这是我的密码: #include<iostream> #include<cmath> using namespace std; int main() { double YearlyInt, LoanAmount, Payment, AmountPaid, MonthlyInt; int NumPayments; cout << "Enter the loan
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
double YearlyInt, LoanAmount, Payment, AmountPaid, MonthlyInt;
int NumPayments;
cout << "Enter the loan amount (LoanAmount) --> ";
cin >> LoanAmount;
cout << "Enter the YEARLY interest rate as a percentage --> ";
cin >> YearlyInt;
cout << "Enter number of payments --> ";
cin >> NumPayments;
cout << "Loan amount: " << LoanAmount << endl;
cout << "Yearly Interest Rate: " << YearlyInt << "%" << endl;
cout << "Number of Payments: " << NumPayments << endl;
MonthlyInt = YearlyInt / 12;
Payment = MonthlyInt * pow (( 1 + MonthlyInt ), NumPayments) / (pow(( 1 + MonthlyInt), NumPayments) -1) * LoanAmount;
cout << "Monthly Payment: " << Payment << endl;
AmountPaid = Payment * 36;
cout << "Amount Paid Back: " << AmountPaid << endl;
cout << "Interest Paid: " << (AmountPaid - LoanAmount) << endl;
cout << "Program Over" << endl << endl << endl << endl;
cout << "Press Enter to end -->" << endl;
return 0;
}
这是我得到的输出:
Enter the loan amount (LoanAmount) --> 10000
Enter the YEARLY interest rate as a percentage --> 12
Enter number of payments --> 36
Loan amount: 10000
Yearly Interest Rate: 12%
Number of Payments: 36
Monthly Payment: 10000
Amount Paid Back: 360000
Interest Paid: 350000
Program Over
Press Enter to end -->
Press any key to continue . . .
正如你所看到的,贷款金额显然是错误的。如何修复代码?程序应该计算复利还是单利
你的计算似乎不正确。你计算一个月利率,这使它看起来像单利,但你使用pow,这表明与复利有关。您可能应该对此进行调查。有几个问题:
MonthlyInt/100.0
#包括
#包括
使用名称空间std;
int main()
{
双倍年薪、贷款金额、付款、已付金额、月收入、金额;
cout>LoanAmount;
cout>YearlyInt;
金额;
cout这里有一个程序,可以正确计算付款,假设如下:
- 年利息按月复利计算
- 贷款不收取任何费用
- 贷款发放后一个月开始还款
- 每月付款金额不变
- 没有错过每月付款
#包括
#包括
使用名称空间std;
int main()
{
双倍年薪、贷款金额、付款、已付款金额、月工资;
国际货币基金组织;
cout>LoanAmount;
cout>YearlyInt;
金额;
cout第1步:MonthlyInt
由于复利的影响,不等于YearlyInt/12
。在较小期间的利率和较大期间的等值利率之间转换的一般公式为:(1+r)^n=1+R。因此,在本例中,R=MonthlyInt和R=YearlyInt。因此,第一个业务顺序是更改
发件人:
致:
步骤2:添加一行打印MonthlyInt
,以便您可以验证计算结果。:)
步骤3:将AmountPaid=Payment*36;
更改为amountpayment=Payment*NumPayments;
步骤4:可选地,添加美元符号并清除小数
我们必须添加标题#include
,然后使用cout>YearlyInt设置小数位数;
金额;
不能包含
#包括
使用名称空间std;
int main()
{
双倍年薪、贷款金额、付款、已付金额、月收入、金额;
cout>LoanAmount;
cout>YearlyInt;
金额;
cout如果您不想使用pow函数,您可以自己直接计算级数的几何和和和第n项(pow()来自几何项和和)。请参见此处的证明:
int月=60;
浮动原则=10000;
浮动率=6.5f/100;
每月浮动系数=1+费率/12;
浮动温度=1,温度2=月系数;
对于(int i=0;i
“未给出正确答案”不是真的告诉我们你期望它做什么。你现在得到了什么,你想得到什么?对不起,我以后会更清楚。只需编辑问题并添加信息。不必抱歉,只需要修复问题!我打赌如果你告诉我们真正的问题,人们的否决票会重新添加。你的程序mming看起来不错,但你的数学有问题。例如,为什么AmountPaid
总是由36笔付款组成?请看我是如何编辑你的问题的。不要输入任何与个人无关的文本-这会让人反感;格式化你的代码;制作一个清晰反映你的问题的标题。尝试在文章中使用更好的结构谢谢,我把MonthlyInt公式改为MonthlyInt=(YearlyInt/100.0)/12;我刚刚看到我在lol中硬编码了36,我将把它去掉。现在可以了!我认为他在程序中的任何地方都不会执行整数除法。@andrew.punnett我以为monthlynt在第一次修订中是整数,但事实并非如此,是的,没有……我将编辑答案。欢迎使用堆栈溢出!而这个代码片段可以解决这个问题ion,确实有助于提高您文章的质量。请记住,您将在将来回答读者的问题,而这些人可能不知道您的代码建议的原因。还请尽量不要用解释性注释挤满您的代码,这会降低代码和解释的可读性!
Enter the loan amount (LoanAmount) --> 10000
Enter the YEARLY interest rate as a percentage --> 12
Enter number of payments --> 36
Loan amount: 10000
Yearly Interest Rate: 12%
Number of Payments: 36
Monthly Payment: 10000
Amount Paid Back: 360000
Interest Paid: 350000
Program Over
Press Enter to end -->
Press any key to continue . . .
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
double YearlyInt, LoanAmount, Payment, AmountPaid, MonthlyInt, NumPayments;
cout << "Enter the loan amount (LoanAmount) --> ";
cin >> LoanAmount;
cout << "Enter the YEARLY interest rate as a percentage --> ";
cin >> YearlyInt;
cout << "Enter number of payments --> ";
cin >> NumPayments;
cout << "Loan amount: " << LoanAmount << endl;
cout << "Yearly Interest Rate: " << YearlyInt << "%" << endl;
cout << "Number of Payments: " << NumPayments << endl;
MonthlyInt = (YearlyInt/100.0) / 12;
Payment = MonthlyInt * pow (( 1 + MonthlyInt ), NumPayments) / (pow(( 1 + MonthlyInt), NumPayments) -1) * LoanAmount;
cout << "Monthly Payment: " << Payment << endl;
AmountPaid = Payment * 36;
cout << "Amount Paid Back: " << AmountPaid << endl;
cout << "Interest Paid: " << (AmountPaid - LoanAmount) << endl;
cout << "Program Over" << endl << endl << endl << endl;
cout << "Press Enter to end -->" << endl;
return 0;
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
double YearlyInt, LoanAmount, Payment, AmountPaid, MonthlyInt;
int NumPayments;
cout << "Enter the loan amount (LoanAmount) --> ";
cin >> LoanAmount;
cout << "Enter the YEARLY interest rate as a percentage --> ";
cin >> YearlyInt;
cout << "Enter number of monthly payments --> ";
cin >> NumPayments;
cout << "Loan amount: " << LoanAmount << endl;
cout << "Yearly Interest Rate: " << YearlyInt << "%" << endl;
cout << "Number of Monthly Payments: " << NumPayments << endl;
MonthlyInt = pow( 1 + YearlyInt/100, 1.0/12 );
Payment = LoanAmount * pow( MonthlyInt, NumPayments ) *
( MonthlyInt - 1 ) /
( pow( MonthlyInt, NumPayments ) - 1 );
cout << "Monthly Payment: " << Payment << endl;
AmountPaid = Payment * NumPayments;
cout << "Amount Paid Back: " << AmountPaid << endl;
cout << "Interest Paid: " << (AmountPaid - LoanAmount) << endl;
cout << "Program Over" << endl << endl << endl << endl;
return 0;
}
MonthlyInt = YearlyInt / 12;
MonthlyInt = pow ( (1.0 + YearlyInt) , (1.0/NumPayments) ) - 1.0; // note decimals!
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
int main()
{
double YearlyInt = -1, LoanAmount = -1, Payment = -1, AmountPaid = -1, MonthlyInt = -1;
int NumPayments;
cout << "Enter the loan amount (LoanAmount) --> ";
cin >> LoanAmount;
cout << "Enter the YEARLY interest rate as a decimal number (e.g. 3.25% as .0325) --> ";
cin >> YearlyInt;
cout << "Enter number of payments --> ";
cin >> NumPayments;
cout << "Loan amount: $" << setprecision(2) << fixed << LoanAmount << endl;
cout << "Yearly Interest Rate: " << setprecision(3) << YearlyInt * 100 << "%" << endl;
cout << "Number of Payments: " << NumPayments << endl;
MonthlyInt = pow ( (1.0 + YearlyInt) , (1.0/NumPayments) ) - 1.0;
cout << "MonthlyInt: " << MonthlyInt*100 << "%" << endl;
Payment = MonthlyInt * pow (( 1 + MonthlyInt ), NumPayments) / (pow(( 1 + MonthlyInt), NumPayments) -1) * LoanAmount;
cout << "Monthly Payment: $" << setprecision(2) << Payment << endl;
AmountPaid = Payment * NumPayments;
cout << "Amount Paid Back: $" << AmountPaid << endl;
cout << "Interest Paid: $" << (AmountPaid - LoanAmount) << endl;
cout << "Program Over" << endl << endl << endl << endl;
cout << "Press Enter to end -->" << endl;
return 0;
}
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
double YearlyInt, LoanAmount, Payment, AmountPaid, MonthlyInt, NumPayments;
cout << "Enter the loan amount (LoanAmount) --> ";
cin >> LoanAmount;
cout << "Enter the YEARLY interest rate as a percentage --> ";
cin >> YearlyInt;
cout << "Enter number of payments --> ";
cin >> NumPayments;
cout << "Loan amount: " << LoanAmount << endl;
cout << "Yearly Interest Rate: " << YearlyInt << "%" << endl;
cout << "Number of Payments: " << NumPayments << endl;
MonthlyInt = (YearlyInt/100.0) / 12;
Payment = MonthlyInt * pow (( 1 + MonthlyInt ), NumPayments) / (pow(( 1 + MonthlyInt), NumPayments) -1) * LoanAmount;
cout << "Monthly Payment: " << Payment << endl;
//correction to Amount paid Value
AmountPaid = Payment * NumPayments;
cout << "Amount Paid Back: " << AmountPaid << endl;
cout << "Interest Paid: " << (AmountPaid - LoanAmount) << endl;
cout << "Program Over" << endl << endl << endl << endl;
cout << "Press Enter to end -->" << endl;
return 0;
}
int months = 60;
float principle = 10000;
float rate = 6.5f/100;
float monthly_factor = 1 + rate / 12;
float temp = 1, temp2= monthly_factor;
for(int i = 0;i < months - 1; i++){ //this for loop calculates the geometric sum with ratio of monthly_factor in temp and pow(monthly_factor,n) in temp2
temp *= monthly_factor;
temp2 *= monthly_factor;
temp += 1;
}
float monthly_payment = principle * temp2 / temp;