用 C 语言计算单笔贷款分期偿还步骤

CBeginner
立即练习

简介

在本实验中,你将学习如何用 C 语言计算单笔贷款分期偿还的一个步骤。本实验涵盖了贷款分期偿还计算所需的基本步骤,包括读取本金、利率和还款金额,以及计算还款中的利息部分和本金部分。完成本实验后,你将对金融数学以及使用 C 语言进行利息计算有更深入的理解。

读取本金、利率、还款额

在这一步中,你将学习如何在 C 语言中输入和读取贷款分期偿还计算所需的基本参数:本金、利率和还款额。

首先,让我们为贷款分期偿还程序创建一个 C 源文件:

cd ~/project
nano loan_amortization.c

现在,将以下代码添加到文件中:

#include <stdio.h>

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

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

    // 读取年利率
    printf("Enter the annual interest rate (in percentage): ");
    scanf("%lf", &rate);

    // 读取月还款额
    printf("Enter the monthly payment amount: ");
    scanf("%lf", &payment);

    // 显示输入的值
    printf("\nLoan Details:\n");
    printf("Principal: $%.2f\n", principal);
    printf("Annual Interest Rate: %.2f%%\n", rate);
    printf("Monthly Payment: $%.2f\n", payment);

    return 0;
}

编译并运行程序:

gcc loan_amortization.c -o loan_amortization
./loan_amortization

示例输出:

Enter the loan principal amount: 10000
Enter the annual interest rate (in percentage): 5.5
Enter the monthly payment amount: 200

Loan Details:
Principal: $10000.00
Annual Interest Rate: 5.50%
Monthly Payment: $200.00

计算利息部分和本金部分

在这一步中,你将学习如何使用金融数学公式来计算贷款还款中的利息和本金部分。

打开之前的 loan_amortization.c 文件并进行修改,以包含计算逻辑:

cd ~/project
nano loan_amortization.c

使用计算函数更新代码:

#include <stdio.h>

// 计算月利息的函数
double calculateMonthlyInterest(double principal, double annualRate) {
    double monthlyRate = annualRate / 12 / 100;
    return principal * monthlyRate;
}

// 计算本金部分的函数
double calculatePrincipalPortion(double payment, double monthlyInterest) {
    return payment - monthlyInterest;
}

int main() {
    double principal, rate, payment;
    double monthlyInterest, principalPortion;

    // 之前的输入代码保持不变
    printf("Enter the loan principal amount: ");
    scanf("%lf", &principal);

    printf("Enter the annual interest rate (in percentage): ");
    scanf("%lf", &rate);

    printf("Enter the monthly payment amount: ");
    scanf("%lf", &payment);

    // 计算利息和本金部分
    monthlyInterest = calculateMonthlyInterest(principal, rate);
    principalPortion = calculatePrincipalPortion(payment, monthlyInterest);

    // 显示计算结果
    printf("\nLoan Payment Breakdown:\n");
    printf("Monthly Interest: $%.2f\n", monthlyInterest);
    printf("Principal Portion: $%.2f\n", principalPortion);
    printf("Remaining Principal: $%.2f\n", principal - principalPortion);

    return 0;
}

编译并运行更新后的程序:

gcc loan_amortization.c -o loan_amortization
./loan_amortization

示例输出:

Enter the loan principal amount: 10000
Enter the annual interest rate (in percentage): 5.5
Enter the monthly payment amount: 200

Loan Payment Breakdown:
Monthly Interest: $45.83
Principal Portion: $154.17
Remaining Principal: $9845.83

打印更新后的本金

在这一步中,你将学习如何跟踪并打印每次贷款还款后更新的本金余额,以此展示贷款分期偿还的过程。

打开之前的 loan_amortization.c 文件并进行修改,以包含多次还款迭代:

cd ~/project
nano loan_amortization.c

更新代码以模拟多次贷款还款:

#include <stdio.h>

// 之前的函数保持不变
double calculateMonthlyInterest(double principal, double annualRate) {
    double monthlyRate = annualRate / 12 / 100;
    return principal * monthlyRate;
}

double calculatePrincipalPortion(double payment, double monthlyInterest) {
    return payment - monthlyInterest;
}

int main() {
    double principal, rate, payment;
    int totalPayments;

    // 输入贷款详情
    printf("Enter the loan principal amount: ");
    scanf("%lf", &principal);

    printf("Enter the annual interest rate (in percentage): ");
    scanf("%lf", &rate);

    printf("Enter the monthly payment amount: ");
    scanf("%lf", &payment);

    printf("Enter the total number of payments: ");
    scanf("%d", &totalPayments);

    // 打印初始贷款详情
    printf("\nInitial Loan Details:\n");
    printf("Principal: $%.2f\n", principal);
    printf("Annual Interest Rate: %.2f%%\n", rate);
    printf("Monthly Payment: $%.2f\n\n", payment);

    // 模拟贷款分期偿还
    for (int month = 1; month <= totalPayments; month++) {
        double monthlyInterest = calculateMonthlyInterest(principal, rate);
        double principalPortion = calculatePrincipalPortion(payment, monthlyInterest);

        // 更新本金
        principal -= principalPortion;

        // 打印每月明细
        printf("Payment %d:\n", month);
        printf("Monthly Interest: $%.2f\n", monthlyInterest);
        printf("Principal Portion: $%.2f\n", principalPortion);
        printf("Remaining Principal: $%.2f\n\n", principal);
    }

    return 0;
}

编译并运行更新后的程序:

gcc loan_amortization.c -o loan_amortization
./loan_amortization

示例输出:

Enter the loan principal amount: 10000
Enter the annual interest rate (in percentage): 5.5
Enter the monthly payment amount: 200
Enter the total number of payments: 3

Initial Loan Details:
Principal: $10000.00
Annual Interest Rate: 5.50%
Monthly Payment: $200.00

Payment 1:
Monthly Interest: $45.83
Principal Portion: $154.17
Remaining Principal: $9845.83

Payment 2:
Monthly Interest: $45.04
Principal Portion: $154.96
Remaining Principal: $9690.87

Payment 3:
Monthly Interest: $44.25
Principal Portion: $155.75
Remaining Principal: $9535.12

总结

在本实验中,你将学习如何在 C 语言中读取贷款分期偿还计算所需的基本参数,包括本金、利率和还款金额。你还将学习如何使用金融数学公式来计算贷款还款中的利息和本金部分。完成本实验后,你将对用 C 语言计算单笔贷款分期偿还步骤所涉及的基本步骤有一个基本的了解。