IDENTIFICATION DIVISION.
PROGRAM-ID. SQRT.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
    SELECT INPUT-FILE ASSIGN TO "sqrtFIXED.dat"
    ORGANIZATION IS LINE SEQUENTIAL.
    SELECT STANDARD-OUTPUT ASSIGN TO DISPLAY.
DATA DIVISION.
FILE SECTION.
FD INPUT-FILE.
    01 STANDARD-INPUT PICTURE X(80).
FD STANDARD-OUTPUT.
    01 OUT-LINE  PICTURE X(80).
WORKING-STORAGE SECTION.
77 DIFF PICTURE V9(5).
77 Z    PICTURE 9(11)V9(6).
77 K    PICTURE S9999.
77 X    PICTURE 9(11)V9(6).
77 Y    PICTURE 9(11)V9(6).
77 TEMP PICTURE 9(11)V9(6).
01 IN-CARD.
   02 IN-Z     PICTURE S9(10)V9(6) SIGN LEADING SEPARATE.
   02 IN-DIFF  PICTURE V9(5).
   02 FILLER   PICTURE X(58).
01 TITLE-LINE.
   02 FILLER PICTURE X(9) VALUE SPACES.
   02 FILLER PICTURE X(26) VALUE 'SQUARE ROOT APPROXIMATIONS'.
01 UNDER-LINE.
   02 FILLER PICTURE X(44) VALUE 
      '--------------------------------------------'.
01 COL-HEADS.
   02 FILLER PICTURE X(8) VALUE SPACES.
   02 FILLER PICTURE X(6) VALUE 'NUMBER'.
   02 FILLER PICTURE X(15) VALUE SPACES.
   02 FILLER PICTURE X(11) VALUE 'SQUARE ROOT'.
01 UNDERLINE-2.
   02 FILLER PICTURE X(20) VALUE ' -------------------'.
   02 FILLER PICTURE X(5) VALUE SPACES.
   02 FILLER PICTURE X(19) VALUE '------------------'.
01 PRINT-LINE.
   02 FILLER PICTURE X VALUE SPACE.
   02 OUT-Z  PICTURE Z(11)9.9(6).
   02 FILLER PICTURE X(5) VALUE SPACES.
   02 OUT-Y  PICTURE Z(11)9.9(6).
01 ERROR-MESS.
   02 FILLER PICTURE X VALUE SPACE.
   02 OT-Z   PICTURE -(11)9.9(6).
   02 FILLER PICTURE X(21) VALUE '        INVALID INPUT'.
01 ABORT-MESS.
   02 FILLER PICTURE X VALUE SPACE.
   02 OUTP-Z PICTURE Z(11)9.9(6).
   02 FILLER PICTURE X(37) VALUE
      '  ATTEMPT ABORTED,TOO MANY ITERATIONS'.

PROCEDURE DIVISION.
    OPEN INPUT INPUT-FILE, OUTPUT STANDARD-OUTPUT.
    WRITE OUT-LINE FROM TITLE-LINE AFTER ADVANCING 0 LINES.
    WRITE OUT-LINE FROM UNDER-LINE AFTER ADVANCING 1 LINE.
    WRITE OUT-LINE FROM COL-HEADS AFTER ADVANCING 1 LINE.
    WRITE OUT-LINE FROM UNDERLINE-2 AFTER ADVANCING 1 LINE.
S1.  
    READ INPUT-FILE INTO IN-CARD AT END GO TO FINISH.
    IF IN-Z IS GREATER THAN ZERO GO TO B1.
    MOVE IN-Z TO OT-Z.
    WRITE OUT-LINE FROM ERROR-MESS AFTER ADVANCING 1 LINE.
    GO TO S1.
B1. 
    MOVE IN-DIFF TO DIFF.
    MOVE IN-Z TO Z.
    DIVIDE 2 INTO Z GIVING X ROUNDED.
    PERFORM S2 THRU E2 VARYING K FROM 1 BY 1
        UNTIL K IS GREATER THAN 1000.
    MOVE IN-Z TO OUTP-Z.
    WRITE OUT-LINE FROM ABORT-MESS AFTER ADVANCING 1 LINE.
    GO TO S1.
S2. 
    COMPUTE Y ROUNDED = 0.5 * (X + Z / X).
    SUBTRACT X FROM Y GIVING TEMP.
    IF TEMP IS LESS THAN ZERO COMPUTE TEMP = - TEMP.
    IF TEMP / (Y + X) IS GREATER THAN DIFF GO TO E2.
    MOVE IN-Z TO OUT-Z. 
    MOVE Y TO OUT-Y.
    WRITE OUT-LINE FROM PRINT-LINE AFTER ADVANCING 1 LINE.
    GO TO S1.
E2. 
    MOVE Y TO X.
FINISH.
    CLOSE INPUT-FILE, STANDARD-OUTPUT. 
STOP RUN.