C ++抵押计算器不正确的公式

时间:2020-04-08 11:20:11

标签: c++ math

我正在努力使用此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

2 个答案:

答案 0 :(得分:0)

您当前的问题是:

  1. 您不输出monthlyPayments。您输出annualInterestRate,而应该输出monthlyPayments
  2. 您没有使用正确的公式来计算月付款。参见wrote

对于本金(资本),c,定期(每月)利率,r,周期数(月),n,您的定期付款(每月付款),{ {1}}是:

p

p = ( c * r * ((1 + r)^n) ) / ( ((1 + r)^n) - 1 ) 是已经计算出的功率因数。因此,您可以在最终计算中使用它,以便于阅读。

答案 1 :(得分:0)

您只需要更新这两行:

  // 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;

使用正确的公式和正确的印刷方式。