贷款计算器
1. 等额本金
当月还款 = (当前未还总额)*(月化利率) + (固定每月还款本金)
例如,借款12万元,分12期还,年化利率为6%(月化利率为0.5%)那么固定每月还款本金为1万元,利息为:
2. 等额本息
每月还款金额固定,设为A;总借款额设为M,月化利率设为x,期数为p,那么有公式:
将a_1带入到a_2,a_2带入到a_3,逐项带入,最后a_p为:
3. 代码
3.1 头文件
//
// Created by HF on 2019/3/3.
//
#ifndef TEST_LOANCALCULATION_H
#define TEST_LOANCALCULATION_H
class LoanCalculation {
public:
/**
* 计算贷款每期需还多少
* @param amount 贷款总额(万元)
* @param period 期数,以月为单位
* @param annualizedInterestRate 年化利率(%)
* @param type 1表示等额本金,2表示等额本息
*/
void calculate(double amount, int period, double annualizedInterestRate, int type);
private:
void calEquPrincipal(double amount, int period, double monthlyInterestRate);
void calEquPrincipalAndInterest(double amount, int period, double monthlyInterestRate);
};
#endif //TEST_LOANCALCULATION_H
3.2 源文件
//
// Created by HF on 2019/3/3.
//
#include <iostream>
#include <string>
#include "LoanCalculation.h"
using namespace std;
void LoanCalculation::calculate(double amount, int period, double annualizedInterestRate, int type) {
if (period <= 0) {
cout<<"期数需要大于0!"<<endl;
return ;
}
amount *= 10000;
double monthlyInterestRate = annualizedInterestRate / 1200;
if (type == 1) {
calEquPrincipal(amount, period, monthlyInterestRate);
}
else if (type == 2) {
calEquPrincipalAndInterest(amount, period, monthlyInterestRate);
}
return ;
}
void LoanCalculation::calEquPrincipal(double amount, int period, double monthlyInterestRate) {
double perioidPrincipal = amount / period;
double interest = amount * monthlyInterestRate;
double descInterest = perioidPrincipal * monthlyInterestRate;
double sumInterest = 0.0;
for (int i = 0; i < period; ++i) {
cout<< "第" + to_string(i+1) + "个月需还: " + to_string(perioidPrincipal + interest) + " 元"<< endl;
sumInterest += interest;
interest -= descInterest;
}
cout<<"总利息: " +to_string(sumInterest) + " 元"<<endl;
return ;
}
void LoanCalculation::calEquPrincipalAndInterest(double amount, int period, double monthlyInterestRate) {
double x1 = pow((1+monthlyInterestRate), period);
double x2 = (x1-1)/monthlyInterestRate;
double res = amount*x1/x2;
cout<<"每个月需还: " + to_string(res)<<endl;
double sumInterest = res*period-amount;
cout<<"总利息: " +to_string(sumInterest) + " 元"<<endl;
return ;
}
3.3 使用
#include <iostream>
#include "LoanCalculation.h"
using namespace std;
int main() {
double amount;
int period;
double annualizedInterestRate;
int type;
cout<<"请输入贷款总额(万元), 期数(月数),年化利率(%)"<<endl;
cin>>amount>>period>>annualizedInterestRate;
LoanCalculation loanCalculation;
cout<<"等额本金:"<<endl;
loanCalculation.calculate(amount, period, annualizedInterestRate, 1);
cout<<endl<<"等额本息:"<<endl;
loanCalculation.calculate(amount, period, annualizedInterestRate, 2);
return 0;
}
3.4 输出结果
12 12 6
等额本金:
第1个月需还: 10600.000000 元
第2个月需还: 10550.000000 元
第3个月需还: 10500.000000 元
第4个月需还: 10450.000000 元
第5个月需还: 10400.000000 元
第6个月需还: 10350.000000 元
第7个月需还: 10300.000000 元
第8个月需还: 10250.000000 元
第9个月需还: 10200.000000 元
第10个月需还: 10150.000000 元
第11个月需还: 10100.000000 元
第12个月需还: 10050.000000 元
总利息: 3900.000000 元
等额本息:
每个月需还: 10327.971565
总利息: 3935.658778 元