program main implicit none character*5::a,b read *,a read *,b if(check_num(a).and.check_num(b)) then print '(a)',"OK" else print '(a)',"NG" end if contains logical function check_num(a) result(ok) character*5::a integer::i,ic,ia,len len = LEN_TRIM(a) if(len.ge.2.and.a(1:1).eq.'0') then ok=.false. return end if do i=1,len ic = ICHAR(a(i:i)) if(ICHAR('0').le.ic.and.ic.le.ICHAR('9')) then cycle else ok=.false. return end if end do read (a,*),ia if(0.le.ia.and.ia.le.12345) then ok=.true. else ok=.false. end if end function check_num end program main