Formatting Issues in Fortran Compounded Interest Program?

  • Context: Fortran 
  • Thread starter Thread starter kathrynag
  • Start date Start date
  • Tags Tags
    Fortran Interest
Click For Summary
SUMMARY

The forum discussion centers on formatting issues in a Fortran program designed to calculate compounded interest. The primary concern is the incorrect declaration of the variable P as an integer, which leads to formatting errors when outputting values. Additionally, the use of the format specifier f3.2 is inadequate for numbers greater than one, resulting in asterisks in the output. The recommended solution is to declare P as a real variable and adjust the format specifiers to accommodate larger values.

PREREQUISITES
  • Understanding of Fortran programming syntax
  • Knowledge of variable types in Fortran (e.g., integer vs. real)
  • Familiarity with Fortran format specifiers (e.g., f3.2, f5.2)
  • Basic concepts of compounded interest calculations
NEXT STEPS
  • Revise the Fortran program to declare P as a real variable
  • Learn about Fortran format specifiers and their implications for output
  • Test the program with various input values to observe formatting behavior
  • Explore best practices for handling user input validation in Fortran
USEFUL FOR

Fortran developers, students learning numerical methods, and anyone involved in financial programming seeking to improve output formatting in their applications.

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.)
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K