identification division.
       program-id. is-numeric-test.
       data division.
       file section.
       working-storage section.

       01  ws-user-input                    pic x(10).
       01  ws-user-input-justified          pic x(10) justified right.


       procedure division.
       main-procedure.
           perform process-plain
           perform process-zero-fill
           perform process-trim
           stop run.


       process-plain.
      *> If alphanumeric value entered has spaces, even if the user entered
      *> just digits, it will not pass the "is numeric" test. (Even if
      *> spaces are only trailing.)
           display "(plain) Enter a value: " with no advancing
           accept ws-user-input

           if ws-user-input is numeric then
               display ws-user-input " is numeric!"
           else
               display ws-user-input " is not numeric."
           end-if

           exit paragraph.



       process-zero-fill.
      *> Right justifying and then filling the spaces with zeros followed
      *> by testing for numeric does work.
           display
               "(right justify, zero fill) Enter another value: "
               with no advancing
           end-display
           accept ws-user-input-justified

           inspect ws-user-input-justified
               replacing leading spaces by '0'

           if ws-user-input-justified is numeric then
               display ws-user-input-justified " is numeric!"
           else
               display ws-user-input-justified " is not numeric."
           end-if

           exit paragraph.



       process-trim.
      *> Using the intrinsic "TRIM" function to remove any spaces in the
      *> input also will pass the "is numeric" test if trimmed data is all
      *> contiguous digits.
           display "(trim) Enter a third value: " with no advancing
           accept ws-user-input

           if function trim(ws-user-input) is numeric then
               display function trim(ws-user-input) " is numeric!"
           else
               display function trim(ws-user-input) " is not numeric."
           end-if

           exit paragraph.

       end program is-numeric-test. 

COBOL online compiler

Write, Run & Share COBOL code online using OneCompiler's COBOL online compiler for free. It’s a reliable and accessible playground to practice and run COBOL code with ease. The compiler supports classic COBOL syntax and is great for learning, teaching, and experimenting with business logic programs.

About COBOL

COBOL (Common Business-Oriented Language) is a high-level programming language developed in the 1950s. It is primarily used in business, finance, and administrative systems for companies and governments. COBOL is known for its English-like syntax and is still widely used in legacy enterprise systems.

Sample Code

The following is a simple COBOL program that prints a greeting:

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
PROCEDURE DIVISION.
    DISPLAY "Hello, OneCompiler!".
    STOP RUN.

Taking inputs

In COBOL, input is typically handled using the ACCEPT keyword. Here’s an example that takes user input and prints it back.

IDENTIFICATION DIVISION.
PROGRAM-ID. GREET.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 USER-NAME PIC A(30).

PROCEDURE DIVISION.
    DISPLAY "Enter your name: ".
    ACCEPT USER-NAME.
    DISPLAY "Hello, " USER-NAME "!".
    STOP RUN.

Syntax Basics

Program Structure

COBOL programs are divided into four divisions:

  • IDENTIFICATION DIVISION: Program metadata
  • ENVIRONMENT DIVISION: Machine/environment details (optional)
  • DATA DIVISION: Variable declarations
  • PROCEDURE DIVISION: Actual program logic

Variables

Variables are declared in the DATA DIVISION using PIC clauses.

01 AGE        PIC 99.
01 NAME       PIC A(20).
01 SALARY     PIC 9(5)V99.

Displaying and Accepting Data

DISPLAY "Welcome to COBOL!".
ACCEPT USER-INPUT.

Conditional Statements

IF AGE >= 18
    DISPLAY "Eligible to vote."
ELSE
    DISPLAY "Not eligible."
END-IF.

Loops (PERFORM)

PERFORM VARYING I FROM 1 BY 1 UNTIL I > 5
    DISPLAY "Count: " I
END-PERFORM.

This guide provides a quick reference to COBOL programming syntax and features. Start coding in COBOL using OneCompiler’s COBOL online compiler today!