    MODULE nlp_ex01_mod
      ! .. Implicit None Statement ..
      IMPLICIT NONE
      ! .. Default Accessibility ..
      PUBLIC
      ! .. Intrinsic Functions ..
      INTRINSIC KIND, PRESENT
      ! .. Parameters ..
      INTEGER, PARAMETER :: wp = KIND(1.0D0)

    CONTAINS

      SUBROUTINE obj_fun(first_call,x,finish,obj_f,obj_grad)
        ! .. Implicit None Statement ..
        IMPLICIT NONE
        ! .. Scalar Arguments ..
        REAL (wp), INTENT (OUT) :: obj_f
        LOGICAL, INTENT (INOUT) :: finish
        LOGICAL, INTENT (IN) :: first_call
        ! .. Array Arguments ..
        REAL (wp), OPTIONAL, INTENT (INOUT) :: obj_grad(:)
        REAL (wp), INTENT (IN) :: x(:)
        ! .. Executable Statements ..

        obj_f = x(1)*x(4)*(x(1)+x(2)+x(3)) + x(3)

        IF (PRESENT(obj_grad)) THEN
          obj_grad(1) = x(4)*(2.0_wp*x(1)+x(2)+x(3))
          obj_grad(2) = x(1)*x(4)
          obj_grad(3) = x(1)*x(4) + 1.0_wp
          obj_grad(4) = x(1)*(x(1)+x(2)+x(3))
        END IF
      END SUBROUTINE obj_fun

      SUBROUTINE con_fun(first_call,x,finish,needc,con_f,con_jac)
        ! .. Implicit None Statement ..
        IMPLICIT NONE
        ! .. Scalar Arguments ..
        LOGICAL, INTENT (INOUT) :: finish
        LOGICAL, INTENT (IN) :: first_call
        ! .. Array Arguments ..
        INTEGER, INTENT (IN) :: needc(:)
        REAL (wp), INTENT (INOUT) :: con_f(:)
        REAL (wp), OPTIONAL, INTENT (INOUT) :: con_jac(:,:)
        REAL (wp), INTENT (IN) :: x(:)
        ! .. Executable Statements ..

        IF (needc(1)>0) con_f(1) = x(1)**2 + x(2)**2 + x(3)**2 + x(4)**2

        IF (needc(2)>0) con_f(2) = x(1)*x(2)*x(3)*x(4)
        IF (PRESENT(con_jac)) THEN
          IF (needc(1)>0) con_jac(1,:) = 2.0_wp*x
          IF (needc(2)>0) THEN
            con_jac(2,1) = x(2)*x(3)*x(4)
            con_jac(2,2) = x(1)*x(3)*x(4)
            con_jac(2,3) = x(1)*x(2)*x(4)
            con_jac(2,4) = x(1)*x(2)*x(3)
          END IF
        END IF
      END SUBROUTINE con_fun

    END MODULE nlp_ex01_mod

    PROGRAM nag_nlp_ex01

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

      ! .. Use Statements ..
      USE nag_examples_io, ONLY : nag_std_in, nag_std_out
      USE nag_nlp, ONLY : nag_nlp_sol
      USE nlp_ex01_mod, ONLY : con_fun, obj_fun, wp
      ! .. Implicit None Statement ..
      IMPLICIT NONE
      ! .. Parameters ..
      INTEGER, PARAMETER :: n = 4, num_nlin_con = 2
      ! .. Local Scalars ..
      REAL (wp) :: obj_f
      ! .. Local Arrays ..
      REAL (wp) :: nlin_lower(num_nlin_con), nlin_upper(num_nlin_con), x(n), &
       x_lower(n), x_upper(n)
      ! .. Executable Statements ..
      WRITE (nag_std_out,*) 'Example Program Results for nag_nlp_ex01'

      READ (nag_std_in,*)          ! Skip heading in data file

      ! Read in problem data
      READ (nag_std_in,*) x_lower
      READ (nag_std_in,*) x_upper
      READ (nag_std_in,*) nlin_lower
      READ (nag_std_in,*) nlin_upper
      READ (nag_std_in,*) x

      ! Solve the problem

      CALL nag_nlp_sol(obj_fun,x,obj_f,num_nlin_con=num_nlin_con, &
       con_fun=con_fun,x_lower=x_lower,x_upper=x_upper,nlin_lower=nlin_lower, &
       nlin_upper=nlin_upper)

    END PROGRAM nag_nlp_ex01
