用 C 语言计算抵押贷款还款额

CCBeginner
立即练习

💡 本教程由 AI 辅助翻译自英文原版。如需查看原文,您可以 切换至英文原版

简介

在本实验中,你将学习如何使用 C 语言编程来计算抵押贷款还款额。该实验涵盖了基本步骤,包括读取本金金额、利率和还款期数,然后应用抵押贷款还款额计算公式来确定每月还款额。在本实验结束时,你将拥有一个可以根据用户输入计算抵押贷款还款额的 C 程序。

该实验将带你完成整个过程,从创建一个新的 C 文件到实现还款额计算并打印结果。这个实践练习将帮助你提升使用 C 语言编程进行金融数学和利息计算的技能。


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("C")) -.-> c/BasicsGroup(["Basics"]) c(("C")) -.-> c/FunctionsGroup(["Functions"]) c(("C")) -.-> c/UserInteractionGroup(["User Interaction"]) c/BasicsGroup -.-> c/variables("Variables") c/FunctionsGroup -.-> c/math_functions("Math Functions") c/UserInteractionGroup -.-> c/user_input("User Input") c/UserInteractionGroup -.-> c/output("Output") subgraph Lab Skills c/variables -.-> lab-435348{{"用 C 语言计算抵押贷款还款额"}} c/math_functions -.-> lab-435348{{"用 C 语言计算抵押贷款还款额"}} c/user_input -.-> lab-435348{{"用 C 语言计算抵押贷款还款额"}} c/output -.-> lab-435348{{"用 C 语言计算抵押贷款还款额"}} end

读取本金、利率和还款期数

在这一步中,我们将学习如何读取计算抵押贷款还款额所需的基本输入值:本金金额、利率和还款期数。

首先,让我们为抵押贷款计算器创建一个新的 C 文件:

cd ~/project
nano mortgage_calculator.c

现在,让我们编写读取输入值的代码:

#include <stdio.h>

int main() {
    double principal, rate;
    int num_payments;

    // 提示并读取本金金额
    printf("Enter the loan principal amount: ");
    scanf("%lf", &principal);

    // 提示并读取年利率
    printf("Enter the annual interest rate (as a decimal): ");
    scanf("%lf", &rate);

    // 提示并读取还款期数
    printf("Enter the total number of payments: ");
    scanf("%d", &num_payments);

    // 打印输入值以进行验证
    printf("\nLoan Details:\n");
    printf("Principal: $%.2f\n", principal);
    printf("Annual Interest Rate: %.2f%%\n", rate * 100);
    printf("Number of Payments: %d\n", num_payments);

    return 0;
}

编译并运行程序:

gcc mortgage_calculator.c -o mortgage_calculator
./mortgage_calculator

示例输出:

Enter the loan principal amount: 200000
Enter the annual interest rate (as a decimal): 0.05
Enter the total number of payments: 360

Loan Details:
Principal: $200000.00
Annual Interest Rate: 5.00%
Number of Payments: 360

让我们来分析一下这段代码:

  • 我们使用double类型来存储本金和利率,以处理小数数值
  • scanf()用于读取用户输入
  • %lf格式说明符用于双精度浮点数
  • %d用于整数类型的还款期数
  • 我们打印输入值以验证它们是否被正确获取

每月还款额 = P×(R×(1 + R)^N)/((1 + R)^N - 1)

在这一步中,我们将使用上一步中的输入值来实现抵押贷款还款额的计算公式。

让我们修改现有的mortgage_calculator.c文件:

cd ~/project
nano mortgage_calculator.c

更新代码以计算每月还款额:

#include <stdio.h>
#include <math.h>

double calculate_mortgage_payment(double principal, double rate, int num_payments) {
    // 月利率
    double monthly_rate = rate / 12;

    // 使用抵押贷款公式计算还款额
    double payment = principal *
        (monthly_rate * pow(1 + monthly_rate, num_payments)) /
        (pow(1 + monthly_rate, num_payments) - 1);

    return payment;
}

int main() {
    double principal, rate;
    int num_payments;

    // 提示并读取本金金额
    printf("Enter the loan principal amount: ");
    scanf("%lf", &principal);

    // 提示并读取年利率
    printf("Enter the annual interest rate (as a decimal): ");
    scanf("%lf", &rate);

    // 提示并读取还款期数
    printf("Enter the total number of payments: ");
    scanf("%d", &num_payments);

    // 计算每月还款额
    double monthly_payment = calculate_mortgage_payment(principal, rate, num_payments);

    // 打印输入值和计算出的还款额
    printf("\nLoan Details:\n");
    printf("Principal: $%.2f\n", principal);
    printf("Annual Interest Rate: %.2f%%\n", rate * 100);
    printf("Number of Payments: %d\n", num_payments);
    printf("Monthly Payment: $%.2f\n", monthly_payment);

    return 0;
}

使用数学库编译程序:

gcc mortgage_calculator.c -o mortgage_calculator -lm

运行程序:

./mortgage_calculator

示例输出:

Enter the loan principal amount: 200000
Enter the annual interest rate (as a decimal): 0.05
Enter the total number of payments: 360

Loan Details:
Principal: $200000.00
Annual Interest Rate: 5.00%
Number of Payments: 360
Monthly Payment: $1073.64

关于抵押贷款还款公式的要点:

  • P是贷款本金金额
  • R是月利率(年利率除以 12)
  • N是还款总期数
  • 使用<math.h>中的pow()函数来计算指数
  • 我们使用-lm进行编译以链接数学库

打印还款额

在这最后一步中,我们将增强抵押贷款计算器,以提供详细的还款明细,并对输出进行格式化,使其更具可读性。

让我们修改mortgage_calculator.c文件,以添加更全面的还款信息:

cd ~/project
nano mortgage_calculator.c

使用改进后的还款打印功能更新代码:

#include <stdio.h>
#include <math.h>

double calculate_mortgage_payment(double principal, double rate, int num_payments) {
    // 月利率
    double monthly_rate = rate / 12;

    // 使用抵押贷款公式计算还款额
    double payment = principal *
        (monthly_rate * pow(1 + monthly_rate, num_payments)) /
        (pow(1 + monthly_rate, num_payments) - 1);

    return payment;
}

void print_payment_details(double principal, double rate, int num_payments, double monthly_payment) {
    double total_payment = monthly_payment * num_payments;
    double total_interest = total_payment - principal;

    printf("\n--- 抵押贷款还款明细 --- \n");
    printf("贷款本金:        $%10.2f\n", principal);
    printf("年利率:          %10.2f%%\n", rate * 100);
    printf("还款总期数:        %d\n", num_payments);
    printf("每月还款额:       $%10.2f\n", monthly_payment);
    printf("还款总额:        $%10.2f\n", total_payment);
    printf("支付的总利息:     $%10.2f\n", total_interest);
}

int main() {
    double principal, rate;
    int num_payments;

    // 提示并读取本金金额
    printf("请输入贷款本金金额:");
    scanf("%lf", &principal);

    // 提示并读取年利率
    printf("请输入年利率(以小数形式):");
    scanf("%lf", &rate);

    // 提示并读取还款期数
    printf("请输入还款总期数:");
    scanf("%d", &num_payments);

    // 计算每月还款额
    double monthly_payment = calculate_mortgage_payment(principal, rate, num_payments);

    // 打印详细的还款信息
    print_payment_details(principal, rate, num_payments, monthly_payment);

    return 0;
}

编译程序:

gcc mortgage_calculator.c -o mortgage_calculator -lm

运行程序:

./mortgage_calculator

示例输出:

请输入贷款本金金额:200000
请输入年利率(以小数形式):0.05
请输入还款总期数:360

--- 抵押贷款还款明细 ---
贷款本金:        $  200000.00
年利率:          5.00%
还款总期数:        360
每月还款额:       $    1073.64
还款总额:        $  386510.40
支付的总利息:     $  186510.40

主要改进点:

  • 添加了一个新函数print_payment_details()用于格式化输出
  • 计算还款总额和总利息
  • 使用格式化来对齐小数数值
  • 提供清晰、可读的抵押贷款明细

总结

在本实验中,我们学习了如何读取计算抵押贷款还款额所需的基本输入值,包括本金金额、利率和还款期数。然后,我们使用这些输入值实现了抵押贷款还款额计算公式,以计算每月还款金额。最后,我们打印出计算出的还款额以显示结果。

本实验涵盖的关键步骤包括:从用户输入中读取本金、利率和还款期数,然后应用抵押贷款还款公式来计算每月还款额。该程序确保正确获取输入值,并清晰地输出贷款详情和计算出的还款额。