Fortran Formatting Issues in Fortran Compounded Interest Program?

  • Thread starter Thread starter kathrynag
  • Start date Start date
  • Tags Tags
    Fortran Interest
AI Thread Summary
The discussion revolves around troubleshooting a program designed to calculate compounded interest. The main issue highlighted is the formatting of variables, particularly the declaration of the principal amount (P) as an integer instead of a real number, which leads to inconsistencies when outputting values. The formatting statement using f3.2 is also problematic, as it restricts the output to numbers less than one, resulting in asterisks for larger values. Suggestions include changing P to a real variable type and adjusting the formatting to accommodate larger numbers, such as using f5.2 or f6.2 to ensure proper display of both positive and negative values. Additionally, running a test program is recommended to illustrate the formatting issues more clearly.
kathrynag
Messages
595
Reaction score
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
 
Technology news on Phys.org
If you could be more precise with what problems in formatting you are encountering, you might attract more interest from members.
 
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.)
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
8
Views
1K
Replies
12
Views
1K
Replies
8
Views
2K
Replies
4
Views
2K
Replies
8
Views
4K
Replies
12
Views
3K
Replies
4
Views
2K
Back
Top