IDENTIFICATION DIVISION.
PROGRAM-ID. MAIN.
PROCEDURE DIVISION.
    DISPLAY "Welcome to Online IDE!"
    DISPLAY "Start coding in COBOL..."
    STOP RUN.

Press Run or Ctrl ↵ to execute main.cob.
Type program input here while it runs.

COBOL

Online COBOL Compiler

main.cob is compiled with GnuCOBOL 3.2 (cobc -x) as free-format COBOL, so code can start in any column and needs no sequence numbers. ACCEPT waits for you to type in the terminal while the program runs.

Try it above

How COBOL runs here

Versions
GnuCOBOL 3.2
Build
cobc -x -free -o a.out main.cob
Run
./a.out
Limits per run
256 MB memory, 15 s of CPU time on half a core, 2 MB of output
Time
Up to 10 minutes a run (30 on a paid plan); waits 2 minutes for input (5 on a paid plan)
Sandbox
Each run gets its own Linux sandbox with no network access. Text files your program writes next to your code open as new tabs.
Good to know
Free-format source.

A COBOL program that reads input

This main.cob adds up the prices you type and shows the total through an edited picture. Note that DISPLAY shows a PIC 9(2) counter with its leading zero:

COBOL
IDENTIFICATION DIVISION.
PROGRAM-ID. MAIN.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 ITEM-COUNT  PIC 9(2).
01 PRICE       PIC 9(5)V99.
01 TOTAL       PIC 9(7)V99 VALUE ZERO.
01 TOTAL-OUT   PIC Z,ZZZ,ZZ9.99.
01 I           PIC 9(2).
PROCEDURE DIVISION.
    DISPLAY "How many items? " WITH NO ADVANCING
    ACCEPT ITEM-COUNT
    PERFORM VARYING I FROM 1 BY 1 UNTIL I > ITEM-COUNT
        DISPLAY "Price of item " I ": " WITH NO ADVANCING
        ACCEPT PRICE
        ADD PRICE TO TOTAL
    END-PERFORM
    MOVE TOTAL TO TOTAL-OUT
    DISPLAY "Total: " TOTAL-OUT
    STOP RUN.
Terminal
$ cobc -x -free -o a.out main.cob && ./a.out
How many items? 3
Price of item 01: 12.50
Price of item 02: 7.25
Price of item 03: 100
Total:       119.75

Input and output in COBOL

  • ACCEPT reads one line into a data item. For a numeric PIC with V99, typing 12.50 or 100 both work.
  • DISPLAY … WITH NO ADVANCING keeps the cursor on the line, for prompts.
  • Edited pictures such as Z,ZZZ,ZZ9.99 drop leading zeros and add the comma and decimal point when you MOVE a number into them.

What isn’t available here

  • Fixed format: the source is read as free format, so columns 1 to 7 aren’t special and * in column 7 isn’t a comment. Use *> for comments.
  • Indexed files, screen sections and XML or JSON: GnuCOBOL is built without them here.

Errors you’ll see

  • “main.cob:9: error: syntax error, unexpected *” Arithmetic inside DISPLAY. Compute the value into a data item first, then display it.

Questions

Which COBOL compiler is this?

GnuCOBOL 3.2, compiling main.cob with cobc -x -free.

Does fixed-format COBOL work?

Not as is: the file is compiled as free format. Remove the sequence numbers and use *> for comments.

How do I read input in COBOL?

ACCEPT NAME reads a line into NAME. Type it in the terminal while the program runs.