Fortran: Lesson 5

Lesson topics

View Demos
# 1 # 2 # 3
Download Demos
# 1 # 2 # 3

Demo 3

								        program demo
c       This program evaluates polynomials. The user inputs the degree
c       and the coefficients of the polynomial, and a point where the
c       polynomial is to be evaluated. A function subprogram evaluates
c       the polynomial at the point.
        real a(0:20), x, poly
        integer n
        character ans*1

        print *, "What is the degree of the polynomial?"
        print *, "(no more than 20, please)"
        read *, n
        do i = 0, n
        print *, "Enter the coefficient of the term of degree", i
        read *, a(i)
        end do
 5      print *, "What is the point x of interest?"
        read *, x
        print *, "The value of the polynomial there is", poly(a,x,n)
        print *
        print *, "Do you want to evaluate at another point (y or n)?"
        read*, ans
        if (ans .eq. "y" .or. ans .eq. "Y") go to 5
        print *, "OK, then - bye"

        function poly(a,x,n)
        real poly, a(0:20), x
        integer n
        poly = a(0)
        xpower = 1
        do i=1, n
                xpower = xpower * x
                poly = poly + a(i) * xpower
        end do