IDENTIFICATION DIVISION. PROGRAM-ID. SimpleCalculator. ENVIRONMENT DIVISION. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-NUM1 PIC 9(5). 01 WS-NUM2 PIC 9(5). 01 WS-NUM3 PIC 9(5). 01 WS-SUM PIC 9(5). 01 WS-AVG PIC 9(5)V9(2). 01 WS-COUNT PIC 9(2) VALUE 3. 01 WS-DISPLAY-AVG PIC 9(5). 01 WS-MESSAGE-1 PIC A(30) VALUE 'Enter the first number: '. 01 WS-MESSAGE-2 PIC A(30) VALUE 'Enter the second number: '. 01 WS-MESSAGE-3 PIC A(30) VALUE 'Enter the third number: '. 01 WS-MESSAGE-4 PIC A(30) VALUE 'The sum of the numbers is: '. 01 WS-MESSAGE-5 PIC A(30) VALUE 'The average of the numbers is: '. PROCEDURE DIVISION. MAIN-PARA. DISPLAY WS-MESSAGE-1. ACCEPT WS-NUM1. DISPLAY WS-MESSAGE-2. ACCEPT WS-NUM2. DISPLAY WS-MESSAGE-3. ACCEPT WS-NUM3. COMPUTE WS-SUM = WS-NUM1 + WS-NUM2 + WS-NUM3. COMPUTE WS-AVG = WS-SUM / WS-COUNT. DISPLAY WS-MESSAGE-4 UPON CONSOLE. DISPLAY WS-SUM UPON CONSOLE. DISPLAY WS-MESSAGE-5 UPON CONSOLE. MOVE WS-AVG TO WS-DISPLAY-AVG. DISPLAY WS-DISPLAY-AVG UPON CONSOLE. STOP RUN. END PROGRAM SimpleCalculator.
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.
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.
The following is a simple COBOL program that prints a greeting:
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
PROCEDURE DIVISION.
DISPLAY "Hello, OneCompiler!".
STOP RUN.
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.
COBOL programs are divided into four divisions:
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.
DISPLAY "Welcome to COBOL!".
ACCEPT USER-INPUT.
IF AGE >= 18
DISPLAY "Eligible to vote."
ELSE
DISPLAY "Not eligible."
END-IF.
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!