      PROGRAM INTdr

      real s
      call ITER(s)
      stop
      end        

      SUBROUTINE BEOLV(a,b,nmax,eps,old)

      integer nmax
      real a,b,eps,old
  
      write(*,*)"Az intervallum kezdopontja:"
      read(*,*)a
      write(*,*)"Az intervallum vegpontja:"
      read(*,*)b
      write(*,*)"Az kis intervallumok szama:"
      read(*,*)nmax
      write(*,*)"A pontossag:"
      read(*,*)eps
      write(*,*)"A kezdoertek:"
      read(*,*)old
      
      return
      end

      SUBROUTINE ITER(s)

      integer nmax,n
      real a,b,eps,old,s
      
      call BEOLV(a,b,nmax,eps,old)
      n=1
10    continue      
       call TRAPEZ(a,b,n,s)
       if(n.gt.5.or.n>nmax) goto 11 
       n=n+1
       old=s 
       goto 10 
11      if(abs(s-old).lt.eps*abs(old)) goto 12  
         write(*,*)"Tul sok lepesig jutottunk."
         goto 13
12      continue 
        write(*,*)s
13       continue
      return
      end

      SUBROUTINE FGV(n,x,f)

      integer n
      real f(n),x

      f(1)=x*x

      return
      end

      SUBROUTINE TRAPEZ(a,b,n,s)

      integer n,it,j
      real a,b,s,del,sum,tnm,x

      m=1
      if(n.ne.1) goto 10
       call FGV(m,a,fa)
       call FGV(m,b,fb)
       s=(0.5)*(b-a)*(fa+fb)
       goto 20
      
10    continue
       it=2**(n-2)
       tnm=it
       del=(b-a)/tnm
       x=a+(0.5)*del
       sum=0
       do j=1,it
          call FGV(m,x,fx)
          sum=sum+fx
          x=x+del*j
       enddo
       s=(0.5)*(s+(b-a)*sum/tnm)   

20    continue
      return
      end
