program Palindrome
  !check if an array reads the same in both directions
  implicit none
  integer,parameter::length=30
  integer,dimension(1:length)::x
  integer::size,tail,head,i

  read(*,*) size,(x(i),i=1,size)
  write(*,*) 'input array:'
  write(*,*) (x(i),i=1,size)

  head=1
  tail=size
  do
    if(head>=tail) exit
    if(x(head)/=x(tail)) exit
    head=head+1
    tail=tail-1
  enddo
  write(*,*)
  if (head>=tail) then
    write(*,*) 'the input array is a palindrome'
  else
    write(*,*) 'the input array is NOT a palindrome'
  endif
endprogram Palindrome
