IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO-WORLD.
data division.
WORKING-STORAGE SECTION.
01  greg-date1.
   03  gd   pic 9(02).
   03  gm   pic 9(02). 
   03  gy   pic 9(04). 
01  julian-date.   
   03  jy   pic 9(04).
   03  jd   pic 9(03).
01  julian-date2.   
   03  jyy   pic 9(04).
   03  jdd   pic 9(03).
01  q       pic 9(04).
01  q1      pic 9(04).
01  r       pic 9(04).
01  r1      pic 9(04).
01  m       pic 9(02).
01  b       pic 9(04).
01  a       pic 9(04).
01  g       pic 9(04).
01  h       pic 9(04).
01  difdays pic 9(06).
01  tdays   pic 9(03).
procedure division.
    accept greg-date1.
    display "date 1 " greg-date1.
    move gy to h
    perform greg-jul.
    display 'date 1 julian-date ' julian-date.
    display "*********************************************"
    move gy to a
    initialize greg-date1 julian-date.
    
    accept greg-date1.
    display greg-date1 " 2 nd date".
    perform greg-jul.
    move julian-date to julian-date2
    display 'date 2 julian-date ' julian-date2.
    display "*********************************************"
     display  gy ' gy  '   h  " H  "
     if gy > h
        move gy to g
        move h to b
     else 
        move gy to b
        move h to g
     end-if
     display " g " g  "  b " b
     perform varying b from 1 by 1 until b > g
        display b
        perform leap-check
        compute difdays = difdays + tdays
        display difdays
     end-perform.    
        stop run.
    
greg-jul.
   perform varying m from 1 by 1 until m = gm
       evaluate true
          when m = 1 or 3 or 5 or 7 or 8 or 10 or 12
                compute jd = 31 + jd
          when m = 4 or 6 or 9 or 11
                compute jd = 30 + jd
          when m = 2
                perform leap-check
          when other 
                display " date invalid"
       end-evaluate
   end-perform.
   add gd to jd.
   move gy to jy.

leap-check.
   divide gy by 4 giving q remainder r .
   divide gy by 400 giving q1 remainder r1.
   divide g by 4 giving q remainder r.
   divide g by 400 giving q1 remainder r1.
      evaluate true
          when ( gy(3 : 2) = 00 or g(3 :2) = 00 ) and r1 = 00 
               display gy "is leap yaer"
               move 366 to tdays
               compute jd = 29 + jd
          when ( gy(3 : 2) not = 00 or g(3 : 2))  and r = 00
              display gy "is leap yaer"
              move 366 to tdays
              compute jd = 29 + jd    
          when other 
              display gy "is not leap yaer"
              move 365 to tdays
              compute jd = 28 + jd
      end-evaluate.
 

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!