```    Program f08bsfe

!     F08BSF Example Program Text

!     Mark 27.0 Release. NAG Copyright 2019.

!     .. Use Statements ..
Use nag_library, Only: nag_wp, x04dbf, zgeqpf, ztrsv, zunmqr
!     .. Implicit None Statement ..
Implicit None
!     .. Parameters ..
Complex (Kind=nag_wp), Parameter :: zero = (0.0E0_nag_wp,0.0E0_nag_wp)
Integer, Parameter               :: nin = 5, nout = 6
!     .. Local Scalars ..
Real (Kind=nag_wp)               :: tol
Integer                          :: i, ifail, info, k, lda, ldb, ldx,    &
lwork, m, n, nrhs
!     .. Local Arrays ..
Complex (Kind=nag_wp), Allocatable :: a(:,:), b(:,:), tau(:), work(:),   &
x(:,:)
Real (Kind=nag_wp), Allocatable  :: rwork(:)
Integer, Allocatable             :: jpvt(:)
Character (1)                    :: clabs(1), rlabs(1)
!     .. Intrinsic Procedures ..
Intrinsic                        :: abs
!     .. Executable Statements ..
Write (nout,*) 'F08BSF Example Program Results'
!     Skip heading in data file
Read (nin,*)
Read (nin,*) m, n, nrhs
lda = m
ldb = m
ldx = m
lwork = 64*n
Allocate (a(lda,n),b(ldb,nrhs),tau(n),work(lwork),x(ldx,nrhs),           &
rwork(2*n),jpvt(n))

!     Read A and B from data file

Read (nin,*)(a(i,1:n),i=1,m)
Read (nin,*)(b(i,1:nrhs),i=1,m)

!     Initialize JPVT to be zero so that all columns are free

jpvt(1:n) = 0

!     Compute the QR factorization of A
!     The NAG name equivalent of zgeqpf is f08bsf
Call zgeqpf(m,n,a,lda,jpvt,tau,work,rwork,info)

!     Choose TOL to reflect the relative accuracy of the input data

tol = 0.01_nag_wp

!     Determine which columns of R to use

loop: Do k = 1, n
If (abs(a(k,k))<=tol*abs(a(1,1))) Then
Exit loop
End If
End Do loop

!     Compute C = (Q**H)*B, storing the result in B

k = k - 1

!     The NAG name equivalent of zunmqr is f08auf
Call zunmqr('Left','Conjugate Transpose',m,nrhs,k,a,lda,tau,b,ldb,work,  &
lwork,info)

!     Compute least squares solution by back-substitution in R*B = C

Do i = 1, nrhs

!       The NAG name equivalent of ztrsv is f06sjf
Call ztrsv('Upper','No transpose','Non-Unit',k,a,lda,b(1,i),1)

!       Set the unused elements of the I-th solution vector to zero

b(k+1:n,i) = zero

End Do

!     Unscramble the least squares solution stored in B

Do i = 1, n
x(jpvt(i),1:nrhs) = b(i,1:nrhs)
End Do

!     Print least squares solution

Write (nout,*)
Flush (nout)

!     ifail: behaviour on error exit
!             =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft
ifail = 0
Call x04dbf('General',' ',n,nrhs,x,ldx,'Bracketed','F7.4',               &
'Least squares solution','Integer',rlabs,'Integer',clabs,80,0,ifail)

End Program f08bsfe
```