C语言实现贷款计算器

等额本金,等额本息数学推导:贷款 买房,利息怎么算?不要被忽悠了!李永乐老师讲等额本金和等额本息

一个心血来潮的研究,避免以后买房被坑。

捣鼓了半天才发现原来支付宝的那个利率是年利率不是月利率,坑了我半天。。。

代码

#include <stdio.h>
#include <math.h>
/**
 * @description: 等额本金
 * @param {double} totalPrincipal 贷款总额
 * @param {int} years  按揭年数
 * @param {double} annualInterestRate 年利率
 * @return {*}
 */
double EqualPrincipalPayment(double totalPrincipal, int years, double annualInterestRate)
{   
    annualInterestRate = annualInterestRate / 100.0;
    int period = years * 12;
    double monthlyInterestRate = annualInterestRate / 12.0;
    double monthlyInterestPayment = totalPrincipal * annualInterestRate;
    double monthlyPrincicalPayment = totalPrincipal / period;
    double remainingPrincical = totalPrincipal;

    printf("贷款方式:等额本金\n");
    printf("贷款总额: %.02f\n", totalPrincipal);
    printf("年利率: %.02f%%\n", annualInterestRate * 100);
    printf("贷款期限: %d 年\n", years);
    
    int count = 1;
    do
    {
        monthlyInterestPayment = remainingPrincical * monthlyInterestRate;
        remainingPrincical -= monthlyPrincicalPayment;
        double monthlyPayment = monthlyPrincicalPayment + monthlyInterestPayment;
        printf("期数: %6d | 剩余本金: %10.02f | 月供: %10.02f | 月供本金: %10.02f | 月供利息: %10.02f\n", count, remainingPrincical, monthlyPayment, monthlyPrincicalPayment, monthlyInterestPayment);
        count++;
    } while (count <= period);
}

// 等额本息
/**
 * @description:
 * @param {double} principal 贷款总额
 * @param {int}  years 按揭年数
 * @param {double} annualInterestRate 年利率
 * @return {*}
 */
void EqualMonthlyPayment(double totalPrincipal, int years, double annualInterestRate) {
    annualInterestRate = annualInterestRate / 100.0;
    int period = years * 12;
    double monthlyInterestRate = annualInterestRate / 12.0;
    //月付推导过程可见:https://www.jianshu.com/p/168ffe04ac20
    double monthlyPayment = totalPrincipal * (monthlyInterestRate * pow(1 + monthlyInterestRate, period)) / (pow(1 + monthlyInterestRate, period) - 1);
    
    printf("贷款方式:等额本息\n");
    printf("贷款总额: %.02f\n", totalPrincipal);
    printf("年利率: %.02f%%\n", annualInterestRate * 100);
    printf("贷款期限: %d 年\n", years);
    printf("每月还款金额: %.02f\n", monthlyPayment);
    
    double remainingPrincipal = totalPrincipal;
    int count = 1;
    do {
        double interestPayment = remainingPrincipal * monthlyInterestRate;
        double principalPayment = monthlyPayment - interestPayment;
        remainingPrincipal -= principalPayment;
        
        printf("期数: %6d | 剩余本金: %10.02f | 月供: %10.02f | 月供本金: %10.02f | 月供利息: %10.02f\n", count, remainingPrincipal, monthlyPayment, principalPayment, interestPayment);
        
        count++;
    } while (count <= period);
}

int main()
{
    EqualPrincipalPayment(100000, 10, 5.0);
    EqualMonthlyPayment(100000, 10, 5.0);
}

测试

程序输出:

贷款方式:等额本金
贷款总额: 100000.00
年利率: 5.00%
贷款期限: 1 年
期数:      1 | 剩余本金:   91666.67 | 月供:    8750.00 | 月供本金:    8333.33 | 月供利息:     416.67
期数:      2 | 剩余本金:   83333.33 | 月供:    8715.28 | 月供本金:    8333.33 | 月供利息:     381.94
期数:      3 | 剩余本金:   75000.00 | 月供:    8680.56 | 月供本金:    8333.33 | 月供利息:     347.22
期数:      4 | 剩余本金:   66666.67 | 月供:    8645.83 | 月供本金:    8333.33 | 月供利息:     312.50
期数:      5 | 剩余本金:   58333.33 | 月供:    8611.11 | 月供本金:    8333.33 | 月供利息:     277.78
期数:      6 | 剩余本金:   50000.00 | 月供:    8576.39 | 月供本金:    8333.33 | 月供利息:     243.06
期数:      7 | 剩余本金:   41666.67 | 月供:    8541.67 | 月供本金:    8333.33 | 月供利息:     208.33
期数:      8 | 剩余本金:   33333.33 | 月供:    8506.94 | 月供本金:    8333.33 | 月供利息:     173.61
期数:      9 | 剩余本金:   25000.00 | 月供:    8472.22 | 月供本金:    8333.33 | 月供利息:     138.89
期数:     10 | 剩余本金:   16666.67 | 月供:    8437.50 | 月供本金:    8333.33 | 月供利息:     104.17
期数:     11 | 剩余本金:    8333.33 | 月供:    8402.78 | 月供本金:    8333.33 | 月供利息:      69.44
期数:     12 | 剩余本金:       0.00 | 月供:    8368.06 | 月供本金:    8333.33 | 月供利息:      34.72
贷款方式:等额本息
贷款总额: 100000.00
年利率: 5.00%
贷款期限: 1 年
每月还款金额: 8560.75
期数:      1 | 剩余本金:   91855.92 | 月供:    8560.75 | 月供本金:    8144.08 | 月供利息:     416.67
期数:      2 | 剩余本金:   83677.90 | 月供:    8560.75 | 月供本金:    8178.02 | 月供利息:     382.73
期数:      3 | 剩余本金:   75465.81 | 月供:    8560.75 | 月供本金:    8212.09 | 月供利息:     348.66
期数:      4 | 剩余本金:   67219.51 | 月供:    8560.75 | 月供本金:    8246.31 | 月供利息:     314.44
期数:      5 | 剩余本金:   58938.84 | 月供:    8560.75 | 月供本金:    8280.67 | 月供利息:     280.08
期数:      6 | 剩余本金:   50623.67 | 月供:    8560.75 | 月供本金:    8315.17 | 月供利息:     245.58
期数:      7 | 剩余本金:   42273.85 | 月供:    8560.75 | 月供本金:    8349.82 | 月供利息:     210.93
期数:      8 | 剩余本金:   33889.25 | 月供:    8560.75 | 月供本金:    8384.61 | 月供利息:     176.14
期数:      9 | 剩余本金:   25469.70 | 月供:    8560.75 | 月供本金:    8419.54 | 月供利息:     141.21
期数:     10 | 剩余本金:   17015.08 | 月供:    8560.75 | 月供本金:    8454.62 | 月供利息:     106.12
期数:     11 | 剩余本金:    8525.23 | 月供:    8560.75 | 月供本金:    8489.85 | 月供利息:      70.90
期数:     12 | 剩余本金:      -0.00 | 月供:    8560.75 | 月供本金:    8525.23 | 月供利息:      35.52

等额本金:

image-20230819180750221

等额本息:

image-20230819180809224

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值