# Fortran 77 test programs by ntz11397

VIEWS: 6 PAGES: 8

• pg 1
```									Fortran 77 Exercise

Hsiu-Pin Chen
January 4th, 2007
Exercise : Calculate the value of pi

• Objective: Computing the value of pi by
integrating the function f(x) = 4 / (1 + x2) from
x=0 to 1:
1
Π = ∫ f(x)dx
0
How to compute it:
• By summing up the area of
each rectangular, we can get
the value of pi.
f(x)
• For each rectangular, the      f(x)

area is f(x)*(1/n), where n is
the number of intervals.
0.5/n   (n-0.5)/n x
1.Read the value of the number of
intervals (n) from keyboard.
2. Write a double precision function
which computes f(x) for you.
3. For i=1 to n, x equals to (i-0.5).
sum = sum + f(x)
4. Multiply the summation by (1/n).
5. Print the value of pi on the screen.
Hints
1. Use text editor “pico” to open a file called
“pi.f”
2. Remember you can only start writing your
code from the 7th column.
3. The first line starts as follows:
program Cal_pi
Hints
Program Cal_pi

implicit none
double precision pi,h,sum,x,f
integer n,I

Calculation

end

double precision function f(a)
double precision a
Calculation
return
end
Did you do it right?
Program Cal_pi
implicit none
double precision pi,h,sum,x,f
integer n,i
print*, "Enter the number of intervals"
if (n .le. 0) goto 10
h = 1.0d0/n
sum = 0.0d0
do i =1,n
x = h * (dble(i) - 0.5d0)
sum = sum + f(x)
enddo
pi = h * sum
print*," pi is approximately: ", pi
10    stop
end