Fortran - compounding interest

In summary, you are trying to write a program for compounded interest, but are having trouble with the formattting. Any ideas?Your program accepts inputs of P, r, n and outputs a value of A. + a value of A.
  • #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
 
Technology news on Phys.org
  • #2
If you could be more precise with what problems in formatting you are encountering, you might attract more interest from members.
 
  • #3
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.)
 

1. What is FORTRAN and how is it related to compounding interest?

FORTRAN (short for Formula Translation) is a programming language commonly used in scientific and engineering applications. It is often used to calculate and model complex mathematical computations, such as compounding interest.

2. How do you calculate compounding interest using FORTRAN?

To calculate compounding interest using FORTRAN, you would first need to define the initial amount, interest rate, and number of periods in your program. Then, you can use a loop to calculate the new balance at the end of each period by multiplying the previous balance by (1 + interest rate) and adding it to the initial amount. This process can be repeated for each period to get the final balance.

3. Can FORTRAN be used to calculate compound interest with varying interest rates?

Yes, FORTRAN can be used to calculate compound interest with varying interest rates. This can be achieved by defining the interest rate as an array and using a loop to iterate through each period and apply the corresponding interest rate to the calculation.

4. Are there any built-in functions in FORTRAN for calculating compound interest?

No, there are no built-in functions in FORTRAN specifically for calculating compound interest. However, there are many mathematical functions and operators that can be used in conjunction with loops and arrays to perform the necessary calculations.

5. How accurate is FORTRAN in calculating compounding interest compared to other programming languages?

FORTRAN is a highly accurate programming language, often used for scientific and engineering applications that require precise calculations. As long as the program is written correctly, FORTRAN can accurately calculate compound interest just like any other programming language.

Similar threads

  • Programming and Computer Science
Replies
4
Views
585
  • Programming and Computer Science
Replies
12
Views
953
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
Replies
16
Views
1K
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
Back
Top