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