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

    CONTAINS

      SUBROUTINE obj_fun(first_call,final_call,x,continue,finish,obj_f, &
         obj_grad,i_comm,r_comm)
        ! .. Implicit None Statement ..
        IMPLICIT NONE
        ! .. Intrinsic Functions ..
        INTRINSIC PRESENT, PRODUCT
        ! .. Scalar Arguments ..
        REAL (wp), INTENT (OUT) :: obj_f
        LOGICAL, INTENT (INOUT) :: continue, finish
        LOGICAL, INTENT (IN) :: final_call, first_call
        ! .. Array Arguments ..
        INTEGER, OPTIONAL, INTENT (IN) :: i_comm(:)
        REAL (wp), OPTIONAL, INTENT (INOUT) :: obj_grad(:)
        REAL (wp), OPTIONAL, INTENT (IN) :: r_comm(:)
        REAL (wp), INTENT (IN) :: x(:)
        ! .. Local Scalars ..
        REAL (wp) :: sixty = 60.0_wp
        REAL (wp) :: two = 2.0_wp
        ! .. Executable Statements ..

        obj_f = two - PRODUCT(x)/(two*sixty)

        IF (PRESENT(obj_grad)) THEN
          obj_grad(1) = -PRODUCT(x(2:5))
          obj_grad(2) = -x(1)*PRODUCT(x(3:5))
          obj_grad(3) = -x(1)*x(2)*x(4)*x(5)
          obj_grad(4) = -x(5)*PRODUCT(x(1:3))
          obj_grad(5) = -PRODUCT(x(1:4))
          obj_grad = obj_grad/(two*sixty)
        END IF
      END SUBROUTINE obj_fun

    END MODULE nlp_sparse_ex02_mod

    PROGRAM nag_nlp_sparse_ex02

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

      ! .. Use Statements ..
      USE nag_examples_io, ONLY : nag_std_in, nag_std_out
      USE nlp_sparse_ex02_mod, ONLY : obj_fun, wp
      USE nag_nlp_sparse, ONLY : nag_nlp_sparse_sol, &
       nag_nlp_sparse_cntrl_init, nag_nlp_sparse_cntrl_wp => &
       nag_nlp_sparse_cntrl_dp
      ! .. Implicit None Statement ..
      IMPLICIT NONE
      ! .. Parameters ..
      INTEGER, PARAMETER :: m = 1, n = 5, num_nlin_obj_var = 5
      ! .. Local Scalars ..
      INTEGER :: i
      REAL (wp) :: obj_f
      TYPE (nag_nlp_sparse_cntrl_wp) :: control
      ! .. Local Arrays ..
      REAL (wp) :: s(m), x(n), x_lower(n), x_upper(n)
      CHARACTER (8) :: names(n+m) = (/ 'Varble 1', 'Varble 2', 'Varble 3', &
       'Varble 4', 'Varble 5', 'DummyRow'/)
      ! .. Executable Statements ..
      WRITE (nag_std_out,*) 'Example Program Results for nag_nlp_sparse_ex02'

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

      READ (nag_std_in,*) (x_lower(i),i=1,n)
      READ (nag_std_in,*) (x_upper(i),i=1,n)
      READ (nag_std_in,*) (x(i),i=1,n)

      ! Initialize control structure and set required control parameters

      CALL nag_nlp_sparse_cntrl_init(control)

      control%major_iter_lim = 25
      control%minor_iter_lim = 10
      control%major_step_lim = 5.0_wp

      ! Solve the problem

      CALL nag_nlp_sparse_sol(x,s,obj_f,obj_fun=obj_fun, &
       num_nlin_obj_var=num_nlin_obj_var,names=names,x_lower=x_lower, &
       x_upper=x_upper,control=control)

    END PROGRAM nag_nlp_sparse_ex02
