kimpro82 / MyLegacyPractice

Old School Party!
1 stars 0 forks source link

Fortran : Compound Interest Calculator 2 #7

Closed kimpro82 closed 11 months ago

kimpro82 commented 11 months ago
kimpro82 commented 11 months ago

by Bard

Here is the updated Fortran 95 code with the two improvements implemented:

program compound_interest_calculator

  implicit none

  real :: principal, rate, time, pmt, dividend, future_value
  logical :: reinvest_pmt

  ! Get principal amount from user
  write(*, "(A)", advance='no') 'Enter the principal amount  : '
  read*, principal

  ! Get interest rate from user
  write(*, "(A)", advance='no') 'Enter the interest rate(%)  : '
  read*, rate

  ! Get number of periods from user
  write(*, "(A)", advance='no') 'Enter the number of periods : '
  read*, time

  ! Get periodic payment from user
  write(*, "(A)", advance='no') 'Enter the Periodic Payment  : '
  read*, pmt

  ! Get reinvestment option from user
  write(*, "(A)", advance='no') 'Reinvest monthly payment? (y/n): '
  read(*, "(A)", advance='no') reinvest_pmt_char
  reinvest_pmt = .lower_case(reinvest_pmt_char) == 'y'

  ! Get dividend amount from user
  write(*, "(A)", advance='no') 'Enter the annual dividend amount: '
  read*, dividend

  ! Calculate deposit and future value
  deposit = principal + pmt * time
  if (reinvest_pmt) then
    future_value = principal * (1 + rate/100) ** time + pmt * ( (1 + rate/100) ** time - 1) / (rate/100)
  else
    future_value = principal * (1 + rate/100) ** time + pmt * time
  endif

  ! Adjust future value based on dividend
  future_value = future_value + (pmt - dividend) * time

  ! Display the calculated results
  write(*, "(A)") " "
  write(*, "(A,F12.2)") "The deposit amount  : ", deposit
  write(*, "(A,F12.2)") "The interest amount : ", future_value - deposit
  write(*, "(A,F12.2)") "The total amount    : ", future_value

end program compound_interest_calculator

Changes made: