!   C05AJF Example Program Text
!   Mark 23 Release. NAG Copyright 2011.

    MODULE c05ajfe_mod

!      C05AJF Example Program Module:
!             Parameters and User-defined Routines

!      .. Use Statements ..
       USE nag_library, ONLY : nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER                  :: nout = 6
    CONTAINS
!dec$ attributes offload : mic :: f
       FUNCTION f(x)

!         .. Implicit None Statement ..
          IMPLICIT NONE
!         .. Function Return Value ..
          REAL (KIND=nag_wp)                  :: f
!         .. Scalar Arguments ..
          REAL (KIND=nag_wp), INTENT (IN)     :: x
!         .. Intrinsic Functions ..
          INTRINSIC                              exp
!         .. Executable Statements ..
          f = exp(-x) - x

          RETURN

!dec$ attributes offload : mic :: f
       END FUNCTION f
    END MODULE c05ajfe_mod
    PROGRAM c05ajfe

!      C05AJF Example Main Program

!      .. Use Statements ..
       USE nag_library, ONLY : c05ajf, nag_wp
       USE c05ajfe_mod, ONLY : f, nout
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Local Scalars ..
       REAL (KIND=nag_wp)                  :: eps, eta, x
       INTEGER                             :: ifail, k, nfmax
!      .. Executable Statements ..
       WRITE (nout,*) 'C05AJF Example Program Results'

       WRITE (nout,*)

LOOP:  DO k = 3, 4
          eps = 10.0E0_nag_wp**(-k)
          x = 1.0E0_nag_wp
          eta = 0.0E0_nag_wp
          nfmax = 200

          ifail = -1
          CALL c05ajf(x,eps,eta,f,nfmax,ifail)

          SELECT CASE (ifail)
          CASE (0)
             WRITE (nout,99999) 'With EPS = ', eps, '   root = ', x
          CASE (:-1)
             EXIT LOOP
          CASE (3,4)
             WRITE (nout,99999) 'With EPS = ', eps, ' final value = ', x
          END SELECT

       END DO LOOP

99999  FORMAT (1X,A,E10.2,A,F14.5)
    END PROGRAM c05ajfe
