- #1
kathrynag
- 598
- 0
I'm trying to write a program for compounded interest, but am having trouble with the formattting. Any ideas? This is what I have:
program interest_calculations
!name, Assignment 5-25, to calculate interest
implicit none
real :: A !future value of the account
real :: r !annual percentage interest rate
integer :: P !initial amount of money
real :: n !number of times compounded
real :: t !number of years
character :: ans = 'y'
write (*,*) 'This program accepts inputs of P, r, n and outputs
+ a value of A.'
do
if (ans.eq.'n') exit
write (*,*) 'Please input the initial amount of money.'
read (*,*) P
do
if (P.gt.0) exit
write (*,*) 'Principal must be positive,
+ please input again.'
read (*,*) P
end do
do
write (*,2) 'Please input the interest rate','.'
2 format(' ',a40,a1)
read (*,*) r
do
if (r.gt.0.or.r.lt.1) exit
write (*,*) 'Interest rate must be a decimal between 0 and 1,
+ please input again.'
read (*,*) r
end do
write (*,5) 'Please input the number of times compounded for','.'
5 format(' ',a44,a1)
read (*,*) n
do
if (n.gt.0) exit
write (*,*) 'The number of times compounded must be greater than 0,
+ please input again.'
read (*,*) n
end do
write (*,10) 'Please input the number of years for','.'
10 format(' ',a36,a1)
read (*,*) t
do
if (t.gt.0) exit
write (*,*) 'Number of years must be positive, please input
+ again.'
read (*,*) t
end do
A=P*(1+r/(n*t))**(n*t)
write (*,*) 'Accum Amount Rate Times Years'
write (*,20) A,P,r,n,t
20 format (' ',2x,f3.2,8x,f3.2,7x,f3.2,6x,f3.2,2x,f3.2)
end do
write (*,30) 'Your total accumulation is $',A,'.'
30 format(' ',a30,f6.2,a1)
write (*,*) 'Do you want to run this program again, y or
+ n?'
read (*,*) ans
do
if (ans.eq.'y'.or.ans.eq.'n') exit
write (*,*) 'Still cannot read? Input again.'
read (*,*) ans
end do
end do
end
program interest_calculations
!name, Assignment 5-25, to calculate interest
implicit none
real :: A !future value of the account
real :: r !annual percentage interest rate
integer :: P !initial amount of money
real :: n !number of times compounded
real :: t !number of years
character :: ans = 'y'
write (*,*) 'This program accepts inputs of P, r, n and outputs
+ a value of A.'
do
if (ans.eq.'n') exit
write (*,*) 'Please input the initial amount of money.'
read (*,*) P
do
if (P.gt.0) exit
write (*,*) 'Principal must be positive,
+ please input again.'
read (*,*) P
end do
do
write (*,2) 'Please input the interest rate','.'
2 format(' ',a40,a1)
read (*,*) r
do
if (r.gt.0.or.r.lt.1) exit
write (*,*) 'Interest rate must be a decimal between 0 and 1,
+ please input again.'
read (*,*) r
end do
write (*,5) 'Please input the number of times compounded for','.'
5 format(' ',a44,a1)
read (*,*) n
do
if (n.gt.0) exit
write (*,*) 'The number of times compounded must be greater than 0,
+ please input again.'
read (*,*) n
end do
write (*,10) 'Please input the number of years for','.'
10 format(' ',a36,a1)
read (*,*) t
do
if (t.gt.0) exit
write (*,*) 'Number of years must be positive, please input
+ again.'
read (*,*) t
end do
A=P*(1+r/(n*t))**(n*t)
write (*,*) 'Accum Amount Rate Times Years'
write (*,20) A,P,r,n,t
20 format (' ',2x,f3.2,8x,f3.2,7x,f3.2,6x,f3.2,2x,f3.2)
end do
write (*,30) 'Your total accumulation is $',A,'.'
30 format(' ',a30,f6.2,a1)
write (*,*) 'Do you want to run this program again, y or
+ n?'
read (*,*) ans
do
if (ans.eq.'y'.or.ans.eq.'n') exit
write (*,*) 'Still cannot read? Input again.'
read (*,*) ans
end do
end do
end