    PROGRAM nag_gamma_fun_ex01

      ! Example Program Text for nag_gamma_fun
      ! NAG fl90, Release 4. NAG Copyright 2000.

      ! .. Use Statements ..
      USE nag_examples_io, ONLY : nag_std_out
      USE nag_gamma_fun, ONLY : nag_gamma, nag_log_gamma, nag_incompl_gamma
      ! .. Implicit None Statement ..
      IMPLICIT NONE
      ! .. Intrinsic Functions ..
      INTRINSIC KIND
      ! .. Parameters ..
      INTEGER, PARAMETER :: m = 5, n = 9
      INTEGER, PARAMETER :: wp = KIND(1.0D0)
      ! .. Local Scalars ..
      INTEGER :: i
      REAL (wp) :: p, q, y
      ! .. Local Arrays ..
      REAL (wp) :: a(m), x(n)
      ! .. Executable Statements ..
      WRITE (nag_std_out,*) 'Example Program Results for nag_gamma_fun_ex01'

      ! evaluates gamma function
      WRITE (nag_std_out,*)
      WRITE (nag_std_out,*) '      x        gamma(x)'
      x = (/ -1.5_wp, 1.0_wp, 1.25_wp, 1.5_wp, 1.75_wp, 2.0_wp, 5.0_wp, &
       8.0_wp, 10.0_wp/)

      DO i = 1, n

        y = nag_gamma(x(i))

        WRITE (nag_std_out,fmt='(1X,1P,2E12.3)') x(i), y
      END DO

      ! evaluates log gamma function
      WRITE (nag_std_out,*)
      WRITE (nag_std_out,*) '      x      log gamma(x)'
      x = (/ 1.0_wp, 1.25_wp, 1.5_wp, 1.75_wp, 2.0_wp, 5.0_wp, 10.0_wp, &
       20.0_wp, 1000.0_wp/)

      DO i = 1, n

        y = nag_log_gamma(x(i))

        WRITE (nag_std_out,fmt='(1X,1P,2E12.3)') x(i), y
      END DO

      ! evaluates incomplete gamma function
      WRITE (nag_std_out,*)
      WRITE (nag_std_out,*) '         a           x        p(a,x)      q(a,x)'
      a = (/ 2.0_wp, 7.0_wp, 0.5_wp, 20.0_wp, 21.0_wp/)
      x(1:m) = (/ 3.0_wp, 1.0_wp, 99.0_wp, 21.0_wp, 20.0_wp/)

      DO i = 1, m

        p = nag_incompl_gamma(a(i),x(i))
        q = 1.0_wp - p

        WRITE (nag_std_out,fmt='(1X,4F12.4)') a(i), x(i), p, q
      END DO

    END PROGRAM nag_gamma_fun_ex01
