-
Notifications
You must be signed in to change notification settings - Fork 0
/
DepreciationCalculator2.f95
198 lines (149 loc) · 7.78 KB
/
DepreciationCalculator2.f95
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
! Depreciation Calculator 2
! Date: 2024.04.22
!
! This program calculates depreciation using various methods including:
! - Straight-Line Method
! - Declining Balance Method
! - Double Declining Balance Method
! - Sum-of-the-Years' Digits Method
MODULE GlobalFormats
IMPLICIT NONE
! Declare global variables for formatting
CHARACTER(LEN=19) :: ColumnNameFormat
CHARACTER(LEN=25) :: RowContentFormat
END MODULE GlobalFormats
PROGRAM DepreciationCalculator2
USE GlobalFormats
IMPLICIT NONE
DOUBLE PRECISION :: InitialCost, SalvageValue, UsefulLife
DOUBLE PRECISION :: DepreciationRate
INTEGER :: Year
! Set column and row formatting
ColumnNameFormat = "(A6, A16, A16, A16)"
RowContentFormat = "(I6, F16.2, F16.2, F16.2)"
! Display program information
WRITE(*, '(A)', ADVANCE='NO') "Depreciation Calculator Ver.2"
WRITE(*, '(A/)') " (2024.04.22)"
! Get user input
WRITE(*, '(A)', ADVANCE='NO') " Enter the initial cost of the asset : "; READ *, InitialCost
WRITE(*, '(A)', ADVANCE='NO') " Enter the salvage value of the asset : "; READ *, SalvageValue
WRITE(*, '(A)', ADVANCE='NO') " Enter the useful life of the asset (in years) : "; READ *, UsefulLife
WRITE(*, '(A)') " Enter the depreciation rate(%)"
WRITE(*, '(A)', ADVANCE='NO') " (Default: 2 / useful life, ≒ DDB) : "; READ *, DepreciationRate
! Notification of specific details
WRITE(*, '(/A)') "※ In declining balance method and double declining balance depreciation, "
WRITE(*, '(A)') " the salvage value is not recognized in the calculation of the depreciation base."
WRITE(*, '(/)')
! Set default value of the depreciation rate for DB method
IF (DepreciationRate == 0.0) THEN
DepreciationRate = (2 / UsefulLife) * 100
END IF
! Call the custom functions to calculate depreciation
CALL CalculateStraightLine(InitialCost, SalvageValue, UsefulLife)
CALL CalculateDecliningBalance(InitialCost, SalvageValue, UsefulLife, DepreciationRate)
CALL CalculateDoubleDecliningBalance(InitialCost, SalvageValue, UsefulLife)
CALL CalculateSYD(InitialCost, SalvageValue, UsefulLife)
CONTAINS
SUBROUTINE CalculateStraightLine(InitialCost, SalvageValue, UsefulLife)
IMPLICIT NONE
DOUBLE PRECISION, INTENT(IN) :: InitialCost, SalvageValue, UsefulLife
DOUBLE PRECISION :: DepreciationExpense, AccumulatedDepreciation, BookValue
! Initialize accumulatedDepreciation and bookValue
AccumulatedDepreciation = 0.0
BookValue = InitialCost
! Print header
WRITE(*, '(A, /)') "<Straight-Line Method>"
WRITE(*, ColumnNameFormat) "Year", "Depreciation", "Accumulated", "Book"
WRITE(*, ColumnNameFormat) "", "Expense", "Depreciation", "Value"
! Calculate and print depreciation for each year
DO Year = 1, INT(UsefulLife)
DepreciationExpense = (InitialCost - SalvageValue) / UsefulLife
AccumulatedDepreciation = AccumulatedDepreciation + DepreciationExpense
BookValue = BookValue - DepreciationExpense
WRITE(*, RowContentFormat) Year, DepreciationExpense, AccumulatedDepreciation, BookValue
END DO
WRITE(*, '(A)') ""
END SUBROUTINE CalculateStraightLine
SUBROUTINE CalculateDecliningBalance(InitialCost, SalvageValue, UsefulLife, DepreciationRate)
IMPLICIT NONE
DOUBLE PRECISION, INTENT(IN) :: InitialCost, SalvageValue, UsefulLife, DepreciationRate
DOUBLE PRECISION :: DepreciationExpense, AccumulatedDepreciation, BookValue
! Initialize accumulatedDepreciation and bookValue
AccumulatedDepreciation = 0.0
BookValue = InitialCost
! Print header
WRITE(*, '(A, F5.2, A/)') "<Declining Balance Method> (Rate: ", DepreciationRate, "%)"
WRITE(*, ColumnNameFormat) "Year", "Depreciation", "Accumulated", "Book"
WRITE(*, ColumnNameFormat) "", "Expense", "Depreciation", "Value"
! Calculate and print depreciation for each year
DO Year = 1, INT(UsefulLife)
IF (BookValue * (1 - DepreciationRate / 100) >= SalvageValue) THEN
DepreciationExpense = BookValue * DepreciationRate / 100
AccumulatedDepreciation = AccumulatedDepreciation + DepreciationExpense
BookValue = BookValue - DepreciationExpense
ELSE
BookValue = SalvageValue
DepreciationExpense = (InitialCost - SalvageValue) - AccumulatedDepreciation
AccumulatedDepreciation = InitialCost - SalvageValue
END IF
WRITE(*, RowContentFormat) Year, DepreciationExpense, AccumulatedDepreciation, BookValue
IF (BookValue <= SalvageValue) THEN
EXIT
END IF
END DO
WRITE(*, '(A)') ""
END SUBROUTINE CalculateDecliningBalance
SUBROUTINE CalculateDoubleDecliningBalance(InitialCost, SalvageValue, UsefulLife)
IMPLICIT NONE
DOUBLE PRECISION, INTENT(IN) :: InitialCost, SalvageValue, UsefulLife
DOUBLE PRECISION :: DepreciationRate, DepreciationExpense, AccumulatedDepreciation, BookValue
! Initialize accumulatedDepreciation and bookValue
AccumulatedDepreciation = 0.0
BookValue = InitialCost
DepreciationRate = (2 / UsefulLife) * 100
! Print header
WRITE(*, '(A, F5.2, A/)') "<Double Declining Balance Method> (Rate: ", DepreciationRate, "%)"
WRITE(*, ColumnNameFormat) "Year", "Depreciation", "Accumulated", "Book"
WRITE(*, ColumnNameFormat) "", "Expense", "Depreciation", "Value"
! Calculate and print depreciation for each year
DO Year = 1, INT(UsefulLife)
IF (BookValue * (1 - DepreciationRate / 100) >= SalvageValue) THEN
DepreciationExpense = BookValue * DepreciationRate / 100
AccumulatedDepreciation = AccumulatedDepreciation + DepreciationExpense
BookValue = BookValue - DepreciationExpense
ELSE
BookValue = SalvageValue
DepreciationExpense = (InitialCost - SalvageValue) - AccumulatedDepreciation
AccumulatedDepreciation = InitialCost - SalvageValue
END IF
WRITE(*, RowContentFormat) Year, DepreciationExpense, AccumulatedDepreciation, BookValue
IF (BookValue <= SalvageValue) THEN
EXIT
END IF
END DO
WRITE(*, '(A)') ""
END SUBROUTINE CalculateDoubleDecliningBalance
SUBROUTINE CalculateSYD(InitialCost, SalvageValue, UsefulLife)
IMPLICIT NONE
DOUBLE PRECISION, INTENT(IN) :: InitialCost, SalvageValue, UsefulLife
DOUBLE PRECISION :: DepreciationExpense, AccumulatedDepreciation, BookValue
INTEGER :: SumOfYears
! Calculate the sum of the years' digits
SumOfYears = UsefulLife * (UsefulLife + 1) / 2
! Initialize accumulatedDepreciation and bookValue
AccumulatedDepreciation = 0.0
BookValue = InitialCost
! Print header
WRITE(*, '(A, /)') "<Sum-of-the-Years' Digits Method>"
WRITE(*, ColumnNameFormat) "Year", "Depreciation", "Accumulated", "Book"
WRITE(*, ColumnNameFormat) "", "Expense", "Depreciation", "Value"
! Calculate and print depreciation for each year
DO Year = 1, INT(UsefulLife)
DepreciationExpense = (InitialCost - SalvageValue) * (UsefulLife - Year + 1) / SumOfYears
AccumulatedDepreciation = AccumulatedDepreciation + DepreciationExpense
BookValue = BookValue - DepreciationExpense
WRITE(*, RowContentFormat) Year, DepreciationExpense, AccumulatedDepreciation, BookValue
END DO
WRITE(*, '(A)') ""
END SUBROUTINE CalculateSYD
END PROGRAM DepreciationCalculator2