Fortran: Lesson 4

Lesson topics


View Demos
# 1 # 2 # 3 # 4 # 5 # 6
Download Demos
# 1 # 2 # 3 # 4 # 5 # 6

Demo 4

								        program cosinedemo
c       This program gives the value of cos x to 10 decimal places.
c       It works if x is not too large, say between -10 and 10.
c       For larger x round-off error take effect.
        double precision x, term, sum
        integer I
        print *, "Enter a real number x :"
        read *, x

        sum = 0D0
        term = 1D0
        I = 1
        do while (abs(term) .ge. 1.D-12)
                sum = sum + term
                term = - term * x * x / ((2*I-1) * (2*I))
                I = I + 1
        end do

        write (*,5) "The value of cos x to 10 decimal places is : ", sum
 5      format (a,f13.10)
        print *, "Bye"
        end