Fortran: Lesson 6

Lesson topics


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

Demo 3

								        program checkprime      
        integer n
        character result*9
c       This program determines whether an integer is prime. 

        print *, "Please enter a positive integer :"
        read *, n

        call check(n,result)
        print *
        print *, "The integer", n, " is ", result, ". Bye."
        end

        subroutine check(n,result)
        integer n, i, root
        character result*9
        if (n .eq. 1) then
                result = "not prime"
                return
        end if
        root = sqrt(real(n))
        do i = 2, root
                if (mod(n,i) .eq. 0) then
                        result = "not prime"
                        return
                end if
        end do
        result = "prime"
        end