Closed kimpro82 closed 11 months ago
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:
reinvest_pmt
to store the reinvestment option.reinvest_pmt
value and calculate the future value accordingly.
Add a bool parameter to indicate whether periodic installment should be reinvested.If (periodic installment - dividend) is positive, add it to interest; if negative, subtract it from principal.