DEV Community

Cover image for COBOL Tutorial Series: A calculation program - Session 2
Duc Nguyen Thanh
Duc Nguyen Thanh

Posted on

1 1 1

COBOL Tutorial Series: A calculation program - Session 2

Hello. I'm Duke

In the previous article, I showed you how to program and compile COBOL applications on Windows 11 without a mainframe computer through WSL (Debian) and Visual Studio Code.

Now, I will guide you how to write a very simple calculation program, allowing you to calculate the sum of 2 numbers entered from the keyboard.

First, create the calculate.cbi file

       IDENTIFICATION DIVISION.
       PROGRAM-ID. SimpleAddition.

       DATA DIVISION.          
       WORKING-STORAGE SECTION.
       01  Number1    PIC 9(4).
       01  Number2    PIC 9(4).
       01  Result     PIC 9(4).

       PROCEDURE DIVISION.     
       Main-Process.           
           DISPLAY "Enter first number (0-9999): "
           ACCEPT Number1      
           DISPLAY "Enter second number (0-9999): "
           ACCEPT Number2      

           COMPUTE Result = Number1 + Number2
           DISPLAY "The sum of " Number1 " and " Number2 " is " Result
           STOP RUN.
Enter fullscreen mode Exit fullscreen mode

calculate.cbi

Then, run the following commands:

cobc -x calculate.cbl
./calculate
Enter fullscreen mode Exit fullscreen mode

and here is the result

result

Some notes:

PIC 9(4) means:

  • 9: indicates that the variable will contain digits (0-9).
  • (4): specifies that the variable can contain up to 4 digits.

Repository here

Agent.ai Challenge image

Congrats to the Agent.ai Challenge Winners 🏆

The wait is over! We are excited to announce the winners of the Agent.ai Challenge.

From meal planners to fundraising automators to comprehensive stock analysts, our team of judges hung out with a lot of agents and had a lot to deliberate over. There were so many creative and innovative submissions, it is always so difficult to select our winners.

Read more →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay