PROGRAM Depreciation_Table
!-----------------------------------------------------------------------
! Program to calculate and display a depreciation table using one of
! two methods of depreciation:
!      (1) straight-line 
!      (2) sum-of-the-years'-digits
! Variables used are:
!     Price        :  purchase price of item
!     SalvageValue :  and its salvage value
!     Amount       :  amount to be depreciated (Price - SalvageValue)
!     UsefulLife   :  its useful life
!     Method       :  method of depreciation (1 or 2)
!     Depreciation :  amount of depreciation
!     Year         :  number of year in depreciation table
!     Sum          :  1 + 2 + ... + UsefulLife 
!                       (for sum-of-years'-digits method)
!
! Input:  Price, SalvageValue, UsefulLife, and Method 
! Output: Table showing year number and depreciation for that year
!-----------------------------------------------------------------------

  IMPLICIT NONE
  REAL :: Price, SalvageValue, Amount, Depreciation
  INTEGER :: UsefulLife, Method, Year, Sum

  ! Get the information about the item
  PRINT *, "Enter purchase price, useful life, and salvage value:"
  READ *, Price, UsefulLife, SalvageValue

  ! Calculate amount to be depreciated
  Amount = Price - SalvageValue

  ! Get method of depreciation to be used
  PRINT *, "Enter:"
  PRINT *, "   1 for straight-line depreciation"
  PRINT *, "   2 for sum-of-the-years'-digits method"
  READ *, Method

  ! Determine which type of depreciation method to use
  IF (Method == 1) THEN         ! straight-line method
     Depreciation = Amount / UsefulLife
  ELSE                          ! sum-of-the-years'-digits method
     Sum = 0
     DO Year = 1, UsefulLife
        Sum = Sum + Year
     END DO
  END IF

  ! Generate the depreciation table
  PRINT *
  PRINT *, "Year   Depreciation"
  PRINT *, "==================="
  DO Year = 1, UsefulLife
     IF (Method == 2) THEN
        Depreciation = (UsefulLife - Year + 1) * Amount / REAL(Sum)
     END IF
     PRINT *, " ", Year, Depreciation
  END DO

END PROGRAM Depreciation_Table
