# Fortran Fortran - compounding interest

#### kathrynag

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

Related Programming and Computer Science News on Phys.org

#### mathmate

If you could be more precise with what problems in formatting you are encountering, you might attract more interest from members.

#### alphysicist

Homework Helper
Hi kathrynag,

I definitely agree with mathmate; it's not really clear what you are asking about.

However, there are two things I noticed about your program.

First, you have the variable P declared as an integer, but then when you pass it to your formatting statement here:

Code:
write (*,20) A,P,r,n,t
20 format (' ',2x,f3.2,8x,f3.2,7x,f3.2,6x,f3.2,2x,f3.2)
you are passing it as a real variable. It seems to me that it would be better to declare it as a real variable (even if you only use whole number dollar amounts for P).

Also, I don't think this formatting statement will do what you want it to (and perhaps this is what you are asking about?). You have the variable fields all set to

f3.2

The quick idea behind this is that it set aside 3 spaces total to write the number, and reserves 2 of those spaces for the decimal part. So f3.2 will only work for numbers that are less than one (because once you account for the two spaces to the right of the decimal, you still need one space for the decimal point, and that takes up all three spaces you set aside for the field). So if your value was greater than one, you will just end up with three asterisks because your number can't fit.

Try running this program and comparing the output with the values in the code:

Code:
program test

write (*,20) 12345., 1234.5,123.45
write(*,20)  12.345,1.2345,.12345
write(*,20) .012345,.001234
write(*,20) -12.345

20 format (' ',2x,f5.2,8x,f5.2,7x,f5.2,6x,f5.2,2x,f5.2)

end
None of the numbers in the first write line work, because f5.2 saves 2 spaces for the decimal part, and then after the space for the decimal point there are only two spaces left. The numbers in the next two write lines do work (but they round off the decimal part). Also, the last line does not work because the number is negative, and so requires a space for the negative sign, so it would need to be f6.2, or f7.3, etc.

(If your compiler is set up differently you might get different results than what I am seeing, so please do clarify your question.)

### Physics Forums Values

We Value Quality
• Topics based on mainstream science
• Proper English grammar and spelling
We Value Civility
• Positive and compassionate attitudes
• Patience while debating
We Value Productivity
• Disciplined to remain on-topic
• Recognition of own weaknesses
• Solo and co-op problem solving