Compute Depreciation (Straight Line) in C

CCBeginner
Practice Now

Introduction

In this lab, you will learn how to compute the annual depreciation of an asset using the straight-line method in C programming. You will start by reading the essential parameters, including the asset cost, salvage value, and useful life. Then, you will implement the depreciation formula to calculate the annual depreciation amount. Finally, you will print the calculated annual depreciation.

The lab covers the fundamental concepts of financial math and interest calculations, which are essential for various business and accounting applications. By completing this lab, you will gain practical experience in applying C programming to solve real-world financial problems.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("`C`")) -.-> c/UserInteractionGroup(["`User Interaction`"]) c(("`C`")) -.-> c/BasicsGroup(["`Basics`"]) c(("`C`")) -.-> c/FunctionsGroup(["`Functions`"]) c/UserInteractionGroup -.-> c/output("`Output`") c/BasicsGroup -.-> c/variables("`Variables`") c/UserInteractionGroup -.-> c/user_input("`User Input`") c/FunctionsGroup -.-> c/math_functions("`Math Functions`") subgraph Lab Skills c/output -.-> lab-435344{{"`Compute Depreciation (Straight Line) in C`"}} c/variables -.-> lab-435344{{"`Compute Depreciation (Straight Line) in C`"}} c/user_input -.-> lab-435344{{"`Compute Depreciation (Straight Line) in C`"}} c/math_functions -.-> lab-435344{{"`Compute Depreciation (Straight Line) in C`"}} end

Read Cost, Salvage Value, Useful Life

In this step, you will learn how to input and store the essential parameters for calculating asset depreciation using the straight-line method in C programming.

First, create a new C file for the depreciation calculation:

cd ~/project
nano depreciation.c

Now, add the following code to read the input values:

#include <stdio.h>

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

    // Prompt user to enter asset cost
    printf("Enter the asset cost: ");
    scanf("%f", &cost);

    // Prompt user to enter salvage value
    printf("Enter the salvage value: ");
    scanf("%f", &salvage_value);

    // Prompt user to enter useful life
    printf("Enter the useful life (in years): ");
    scanf("%f", &useful_life);

    // Display the input values
    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;
}

Compile and run the program:

gcc depreciation.c -o depreciation
./depreciation

Example output:

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

Depreciation = (Cost - Salvage)/Life

In this step, you will calculate the annual depreciation using the straight-line method by implementing the depreciation formula in C.

Modify the previous depreciation.c file to include the depreciation calculation:

cd ~/project
nano depreciation.c

Update the code with the depreciation calculation:

#include <stdio.h>

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

    // Prompt user to enter asset cost
    printf("Enter the asset cost: ");
    scanf("%f", &cost);

    // Prompt user to enter salvage value
    printf("Enter the salvage value: ");
    scanf("%f", &salvage_value);

    // Prompt user to enter useful life
    printf("Enter the useful life (in years): ");
    scanf("%f", &useful_life);

    // Calculate annual depreciation
    annual_depreciation = (cost - salvage_value) / useful_life;

    // Display the input values and calculated depreciation
    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);

    // Display the annual depreciation
    printf("\nAnnual Depreciation: $%.2f\n", annual_depreciation);

    return 0;
}

Compile and run the updated program:

gcc depreciation.c -o depreciation
./depreciation

Example output:

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

Print Annual Depreciation

In this step, you will enhance the depreciation calculation program to print a detailed depreciation schedule showing the annual depreciation and book value for each year of the asset's useful life.

Modify the depreciation.c file to include a comprehensive depreciation schedule:

cd ~/project
nano depreciation.c

Update the code to print the annual depreciation schedule:

#include <stdio.h>

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

    // Prompt user to enter asset cost
    printf("Enter the asset cost: ");
    scanf("%f", &cost);

    // Prompt user to enter salvage value
    printf("Enter the salvage value: ");
    scanf("%f", &salvage_value);

    // Prompt user to enter useful life
    printf("Enter the useful life (in years): ");
    scanf("%f", &useful_life);

    // Calculate annual depreciation
    annual_depreciation = (cost - salvage_value) / useful_life;

    // Print depreciation schedule header
    printf("\nDepreciation Schedule:\n");
    printf("---------------------------------------------\n");
    printf("Year\tBeginning Value\tDepreciation\tEnding Value\n");
    printf("---------------------------------------------\n");

    // Initialize book value
    book_value = cost;

    // Print depreciation for each year
    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);

        // Update book value
        book_value -= annual_depreciation;
    }

    return 0;
}

Compile and run the updated program:

gcc depreciation.c -o depreciation
./depreciation

Example output:

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

Summary

In this lab, you learned how to input and store the essential parameters for calculating asset depreciation using the straight-line method in C programming. You then implemented the depreciation formula to calculate the annual depreciation. The key steps involved reading the asset cost, salvage value, and useful life, and then computing the annual depreciation based on the formula (Cost - Salvage)/Life.

Other C Tutorials you may like