C 言語による単一ローン償還ステップの計算

CBeginner
オンラインで実践に進む

はじめに

この実験では、C 言語で単一のローン償還ステップを計算する方法を学びます。この実験では、元金、金利、支払額を読み込むこと、および支払いの利息部分と元金部分の計算を含む、ローン償還計算に必要な基本的なステップを網羅しています。この実験の終わりまでに、C プログラミングを用いた財務数学と利息計算についてのより深い理解を得られるでしょう。

元金、金利、支払額の入力

このステップでは、C 言語でローン償還計算に必要な基本的なパラメータ(元金、金利、支払額)を入力および読み込む方法を学びます。

まず、ローン償還プログラムの C ソースファイルを作成しましょう。

cd ~/project
nano loan_amortization.c

次に、以下のコードをファイルに追加します。

#include <stdio.h>

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

    // 元金の入力
    printf("ローン元金を入力してください:");
    scanf("%lf", &principal);

    // 年利の入力(パーセントで)
    printf("年利を入力してください(パーセントで): ");
    scanf("%lf", &rate);

    // 月々の支払額の入力
    printf("月々の支払額を入力してください:");
    scanf("%lf", &payment);

    // 入力値の表示
    printf("\nローン詳細:\n");
    printf("元金:$%.2f\n", principal);
    printf("年利:%.2f%%\n", rate);
    printf("月々支払額:$%.2f\n", payment);

    return 0;
}

プログラムをコンパイルして実行します。

gcc loan_amortization.c -o loan_amortization
./loan_amortization

実行例:

ローン元金を入力してください:10000
年利を入力してください(パーセントで): 5.5
月々の支払額を入力してください:200

ローン詳細:
元金:$10000.00
年利:5.50%
月々支払額:$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("ローン元金を入力してください:");
    scanf("%lf", &principal);

    printf("年利を入力してください(パーセントで): ");
    scanf("%lf", &rate);

    printf("月々の支払額を入力してください:");
    scanf("%lf", &payment);

    // 利息部分と元金部分を計算
    monthlyInterest = calculateMonthlyInterest(principal, rate);
    principalPortion = calculatePrincipalPortion(payment, monthlyInterest);

    // 計算結果を表示
    printf("\nローン支払いの内訳:\n");
    printf("月利:$%.2f\n", monthlyInterest);
    printf("元金部分:$%.2f\n", principalPortion);
    printf("残高:$%.2f\n", principal - principalPortion);

    return 0;
}

更新されたプログラムをコンパイルして実行します。

gcc loan_amortization.c -o loan_amortization
./loan_amortization

実行例:

ローン元金を入力してください:10000
年利を入力してください(パーセントで): 5.5
月々の支払額を入力してください:200

ローン支払いの内訳:
月利:$45.83
元金部分:$154.17
残高:$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("ローン元金を入力してください:");
    scanf("%lf", &principal);

    printf("年利を入力してください(パーセントで): ");
    scanf("%lf", &rate);

    printf("月々の支払額を入力してください:");
    scanf("%lf", &payment);

    printf("総支払回数を入力してください:");
    scanf("%d", &totalPayments);

    // 初期ローン詳細を表示
    printf("\n初期ローン詳細:\n");
    printf("元金:$%.2f\n", principal);
    printf("年利:%.2f%%\n", rate);
    printf("月々支払額:$%.2f\n\n", payment);

    // ローン償還シミュレーション
    for (int month = 1; month <= totalPayments; month++) {
        double monthlyInterest = calculateMonthlyInterest(principal, rate);
        double principalPortion = calculatePrincipalPortion(payment, monthlyInterest);

        // 元金を更新
        principal -= principalPortion;

        // 月ごとの内訳を表示
        printf("支払回数 %d:\n", month);
        printf("月利:$%.2f\n", monthlyInterest);
        printf("元金部分:$%.2f\n", principalPortion);
        printf("残高:$%.2f\n\n", principal);
    }

    return 0;
}

更新されたプログラムをコンパイルして実行します。

gcc loan_amortization.c -o loan_amortization
./loan_amortization

実行例:

ローン元金を入力してください:10000
年利を入力してください(パーセントで): 5.5
月々の支払額を入力してください:200
総支払回数を入力してください:3

初期ローン詳細:
元金:$10000.00
年利:5.50%
月々支払額:$200.00

支払回数 1:
月利:$45.83
元金部分:$154.17
残高:$9845.83

支払回数 2:
月利:$45.04
元金部分:$154.96
残高:$9690.87

支払回数 3:
月利:$44.25
元金部分:$155.75
残高:$9535.12

まとめ

この実験では、C 言語でローン償還計算に必要な基本的なパラメータ(元金、金利、支払額)の読み込み方法を学びます。また、財務数学の公式を用いて、ローン支払いの利息部分と元金部分を計算する方法も学びます。この実験の終わりまでに、C 言語で単一のローン償還ステップを計算する手順の基本的な理解が得られます。