【问题标题】:C++ Mortgage Calculator Incorrect FormulaC++ 抵押贷款计算器不正确的公式
【发布时间】:2020-07-20 19:00:18
【问题描述】:

我正在努力使用这个 C++ 程序来获得正确的每月付款金额。我认为我无法完全弄清楚的公式有一些地方做错了,它也弄乱了总回报的输出。我需要一些帮助来弄清楚。我在下面的代码中添加了到目前为止的内容:

#include <iostream>
#include <math.h>
using namespace std;

void mortgage::Mortgage() { // class object
    double loanAmount = 0;
    double annualInterestRate = 0;
    double totalYearsToRepay = 0;

  cout << "Enter the amount of the loan:";
  cin >> loanAmount;
  cout << "Enter annual interest rate in decimal term (example 0.075):";
  cin >> annualInterestRate;
  cout << "Enter the length of the loan in years:";
  cin >> totalYearsToRepay;

 cout << "Loan Amount: $" << fixed << loanAmount << endl;
 cout << "Annual Interest Rate: $" << fixed << annualInterestRate << endl;
 cout << "Total Years to Repay: $" << fixed << totalYearsToRepay << endl;

// Find monthly interest rate.
monthlyInterestRate=annualInterestRate /12;

// Find the total # of payments needed.
numberOfPayments = totalYearsToRepay* 12;

// Find power Factor.
 powerFactor = pow(1.0 + monthlyInterestRate, numberOfPayments);

// Find the monthly payment.
monthlyPayment= (loanAmount * monthlyInterestRate)/(1-pow(1 + monthlyInterestRate, totalYearsToRepay));

cout << "Monthly Payment: $" << fixed << annualInterestRate << endl;

    // Find the total pay back
    totalPayBack = (monthlyPayment) * (numberOfPayments);

cout << "Total Pay Back: $"  << fixed << totalPayBack << endl;
};

这段代码我应该得到的输出:

贷款金额:$50000.00
年利率:0.06250
偿还年限:10
每月付款:561.40 美元
总回报:67368.06 美元

这段代码的当前输出:

贷款金额:$50000.000000
年利率:0.062500 美元
总偿还年限:10.000000 美元
每月付款:0.062500 美元
总回报:$-599859.388428

【问题讨论】:

  • 您不应将利率除以 100。(示例对应于 7.5%,但您将其设为 0.075%。)
  • 年利率 X% 小于月利率 (x / 12)%。
  • @idclev463035818:annualInterestRate /12 /100 没有整数除法,因为它被分组为 (annualInterestRate /12) /100
  • 在我的原始程序中,我的月利率 = 年利率/12。很抱歉造成混乱
  • @Batsheba 是的,我的错

标签: c++ math


【解决方案1】:

你只需要更新这两行:

  // Find the monthly payment.
  monthlyPayment= (loanAmount * monthlyInterestRate)/(1-pow(1 + monthlyInterestRate, 
                   totalYearsToRepay));

  cout << "Monthly Payment: $" << fixed << annualInterestRate << endl;

成为

// Find the monthly payment.
double monthlyPayment= (loanAmount * (annualInterestRate / 12) * pow((1 + 
                        annualInterestRate / 12), (totalYearsToRepay * 12))) / (pow((1 
                        + annualInterestRate / 12), (totalYearsToRepay * 12)) - 1);

cout << "Monthly Payment: $" << monthlyPayment << endl;

使用正确的公式和正确的打印。

【讨论】:

  • 欢迎,伙计。如果它适合你,你可以接受答案;)
  • 如何接受?抱歉,我是这个@walid barakat 的新手
  • 你可以看看这里,它也有一些关于堆栈溢出的提示 ^_^ stackoverflow.com/help/someone-answers 我刚刚发现了这个问题,如果你有兴趣,我想它有更多关于你的问题的细节@ 987654322@
【解决方案2】:

您目前的问题是:

  1. 你没有输出monthlyPayments。你在应该输出monthlyPayments的地方输出annualInterestRate
  2. 您没有使用正确的每月付款公式。见here

对于本金(资本)c,定期(每月)利率,r,以及期数(月)n,您的定期付款(每月付款)p 是:

p = ( c * r * ((1 + r)^n) ) / ( ((1 + r)^n) - 1 ) 

(1 + r)^n 是功率因数,您已经计算过了。因此,您可以在最终计算中使用它以便于阅读。

【讨论】:

    猜你喜欢
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-31
    • 2020-12-13
    • 2015-06-30
    • 1970-01-01
    相关资源
    最近更新 更多