PROGRAM pascal
  IMPLICIT NONE
  INTEGER, ALLOCATABLE :: P(:,:)
  INTEGER :: n, i, j
  INTEGER, PARAMETER :: funit = 12

  DO 
     PRINT *, "Dwse diastash:"
     READ *, n
     IF (n > 0) EXIT
     PRINT *, "H diastash prepei na einai thetikh"
  END DO

  ALLOCATE(P(n,n))

  p(:,1) = 1
  p(1,:) = 1

  ! OR
!!%  DO i=1,n
!!%     p(i,1) = 1
!!%     p(1,i) = 1
!!%  END DO

  DO i=2,n
     DO j=2,n
        P(i,j) = P(i-1, j) + P(i,j-1)
     END DO
  END DO

  OPEN(unit=funit, file="pascal.txt", status="new", action="write")

  DO i=1,n
     DO j=1,n
        WRITE (*,"(I6)", advance="no") P(i,j)
        WRITE (funit,"(I6)", advance="no") P(i,j)
     END DO
     WRITE (*,*)
     WRITE (funit,*)
  END DO

  CLOSE(funit)

END PROGRAM pascal
