C 言語による償却計算(直線法)

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

はじめに

この実験では、C プログラミングを使用して、直線法による資産の年間償却額を計算する方法を学びます。資産原価、残存価額、耐用年数などの必須パラメータを読み込むことから始めます。次に、償却式を使用して年間償却額を計算します。最後に、計算された年間償却額を出力します。

この実験は、ビジネスや会計の様々なアプリケーションに不可欠な、財務数学と利息計算の基本概念を網羅しています。この実験を完了することで、C プログラミングを実際の財務問題の解決に適用する実践的な経験を身につけることができます。

原価、残存価額、耐用年数の入力

このステップでは、C プログラミングを用いて直線法による資産償却を計算するために必要なパラメータを入力し、保存する方法を学びます。

まず、償却計算用の新しい C ファイルを作成します。

cd ~/project
nano depreciation.c

次に、入力値を読み取る以下のコードを追加します。

#include <stdio.h>

int main() {
    float cost, salvage_value, useful_life;

    // 資産原価を入力するよう促す
    printf("Enter the asset cost: ");
    scanf("%f", &cost);

    // 残存価額を入力するよう促す
    printf("Enter the salvage value: ");
    scanf("%f", &salvage_value);

    // 耐用年数を入力するよう促す
    printf("Enter the useful life (in years): ");
    scanf("%f", &useful_life);

    // 入力値を表示する
    printf("\nInput Values:\n");
    printf("Asset Cost: $%.2f\n", cost);
    printf("Salvage Value: $%.2f\n", salvage_value);
    printf("Useful Life: %.0f years\n", useful_life);

    return 0;
}

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

gcc depreciation.c -o depreciation
./depreciation

実行例:

Enter the asset cost: 50000
Enter the salvage value: 5000
Enter the useful life (in years): 5

Input Values:
Asset Cost: $50000.00
Salvage Value: $5000.00
Useful Life: 5 years

償却額 = (原価 - 残存価額) / 耐用年数

このステップでは、C 言語で償却式を実装し、直線法による年間償却額を計算します。

前のdepreciation.cファイルに償却計算を追加します。

cd ~/project
nano depreciation.c

償却計算を含むコードを更新します。

#include <stdio.h>

int main() {
    float cost, salvage_value, useful_life, annual_depreciation;

    // 資産原価を入力するよう促す
    printf("Enter the asset cost: ");
    scanf("%f", &cost);

    // 残存価額を入力するよう促す
    printf("Enter the salvage value: ");
    scanf("%f", &salvage_value);

    // 耐用年数を入力するよう促す
    printf("Enter the useful life (in years): ");
    scanf("%f", &useful_life);

    // 年間償却額を計算する
    annual_depreciation = (cost - salvage_value) / useful_life;

    // 入力値と計算された償却額を表示する
    printf("\nInput Values:\n");
    printf("Asset Cost: $%.2f\n", cost);
    printf("Salvage Value: $%.2f\n", salvage_value);
    printf("Useful Life: %.0f years\n", useful_life);

    // 年間償却額を表示する
    printf("\nAnnual Depreciation: $%.2f\n", annual_depreciation);

    return 0;
}

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

gcc depreciation.c -o depreciation
./depreciation

実行例:

Enter the asset cost: 50000
Enter the salvage value: 5000
Enter the useful life (in years): 5

Input Values:
Asset Cost: $50000.00
Salvage Value: $5000.00
Useful Life: 5 years

Annual Depreciation: $9000.00

年間償却額の出力

このステップでは、資産の耐用年数を通して、年間償却額と帳簿価額を示す詳細な償却スケジュールを出力するプログラムを拡張します。

depreciation.c ファイルを修正して、包括的な償却スケジュールを含めます。

cd ~/project
nano depreciation.c

コードを更新して、年間償却スケジュールを出力します。

#include <stdio.h>

int main() {
    float cost, salvage_value, useful_life, annual_depreciation;
    float book_value;
    int year;

    // 資産原価を入力するよう促す
    printf("Enter the asset cost: ");
    scanf("%f", &cost);

    // 残存価額を入力するよう促す
    printf("Enter the salvage value: ");
    scanf("%f", &salvage_value);

    // 耐用年数を入力するよう促す
    printf("Enter the useful life (in years): ");
    scanf("%f", &useful_life);

    // 年間償却額を計算する
    annual_depreciation = (cost - salvage_value) / useful_life;

    // 償却スケジュール見出しを出力する
    printf("\nDepreciation Schedule:\n");
    printf("---------------------------------------------\n");
    printf("Year\tBeginning Value\tDepreciation\tEnding Value\n");
    printf("---------------------------------------------\n");

    // 帳簿価額を初期化する
    book_value = cost;

    // 各年の償却額を出力する
    for (year = 1; year <= useful_life; year++) {
        printf("%d\t$%.2f\t\t$%.2f\t\t$%.2f\n",
               year,
               book_value,
               annual_depreciation,
               book_value - annual_depreciation);

        // 帳簿価額を更新する
        book_value -= annual_depreciation;
    }

    return 0;
}

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

gcc depreciation.c -o depreciation
./depreciation

実行例:

Enter the asset cost: 50000
Enter the salvage value: 5000
Enter the useful life (in years): 5

Depreciation Schedule:
---------------------------------------------
Year	Beginning Value	Depreciation	Ending Value
---------------------------------------------
1	$50000.00		$9000.00	$41000.00
2	$41000.00		$9000.00	$32000.00
3	$32000.00		$9000.00	$23000.00
4	$23000.00		$9000.00	$14000.00
5	$14000.00		$9000.00	$5000.00

まとめ

この実験では、C プログラミングを用いて直線法による資産償却を計算するために必要なパラメータを入力し、保存する方法を学びました。その後、償却式を実装して年間償却額を計算しました。主な手順は、資産原価、残存価額、耐用年数を読み取り、式 (原価 - 残存価額) / 耐用年数に基づいて年間償却額を計算することでした。