Program perfect_square !------------------------------------------------------------------------------ !Program: Perfect Square Test Function !Author: Chris Harper !Date: 2/24/2008 !------------------------------------------------------------------------------ IMPLICIT NONE !declare functions and variables logical :: isPerfectSquare integer :: n !test perfect square function with user input do print *, "Enter an integer (-1 to exit):" read *, n if (n < 0) exit if (isPerfectSquare(n)) then print *, n, " is a perfect square" else print *, n, " is not a perfect square" end if end do end !returns true if n is a perfect square, false otherwise logical function isPerfectSquare(n) IMPLICIT NONE integer , intent (IN) :: n real :: root !if the square root has no decimal portion !test this with 'if sqrt == truncated decimal sqrt' root = sqrt(real(n)) if (root == aint(root)) then isPerfectSquare = .true. else isPerfectSquare = .false. end if end function