Program: Adding two numbers on the screen

       IDENTIFICATION DIVISION.
       PROGRAM-ID. ADDPROG.
       AUTHOR. GROCER.
      *ENVIRONMENT DIVISION.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  INPUT-AREA.
           05  GET-ACCEPT-ANS           PIC X       VALUE SPACES.
           05  FIRST-NUMBER             PIC 999     VALUE 0.
           05  SECOND-NUMBER            PIC 999     VALUE 0.
           05  ADD-ANS                  PIC 9999    VALUE 0.
       PROCEDURE DIVISION.
       MAINLINE.
           DISPLAY "ENTER THE FIRST NUMBER (UNDER 1000)".
           ACCEPT FIRST-NUMBER.
           DISPLAY "ENTER THE SECOND NUMBER (UNDER 1000)".
           ACCEPT SECOND-NUMBER.
           ADD FIRST-NUMBER TO SECOND-NUMBER
               GIVING ADD-ANS.
           DISPLAY "THE ANSWER IS " ADD-ANS.
           DISPLAY "PRESS ENTER TO END THE PROGRAM".
           ACCEPT GET-ACCEPT-ANS.
           STOP RUN.

 

In this program, I decided to have the user enter two numbers on the screen. The program would then add the numbers together and display the answer.

If I am going to have the user enter two numbers, I need a place to store the numbers that they key in. I set up two areas in the WORKING-STORAGE SECTION and named them FIRST-NUMBER and SECOND-NUMBER. I decided that I did not want the user entering more than a 3-digit number so I decided to make the PIC three digits long. Note that in the picture, I can use 9 to indicate numeric data only. Therefore the PIC 999 means three characters of numeric data.

In this program, I am giving the things in the WORKING-STORAGE SECTION an initial value. Note that initial value means the value when the program is started. Any changes made to the work area as the program is running will change the initial value. In this program I gave all of the numeric fields an initial value of zero.

There is a third numeric field called ADD-ANS where I will put the answer after the computer has done the addition. Note that this field has PIC 9999 because when you add two three-digit numbers together, the answer can be 4 digits.

Remember, I want the user to press enter when they have finished looking at the answer so I have the field called GET-ACCEPT-ANS. This field has a PIC X so it can one character of any data. Because I am giving initial values in this program, I decided to give this field an initial value of SPACE. SPACE is a reserved word that means just that, blank. I could also have given the value as VALUE " ". That is a space enclosed in quotes. It would have then taken the initial value as the single space enclosed in the quotes.

       01  INPUT-AREA.
           05  GET-ACCEPT-ANS           PIC X       VALUE SPACES.
           05  FIRST-NUMBER             PIC 999     VALUE 0.
           05  SECOND-NUMBER            PIC 999     VALUE 0.
           05  ADD-ANS                  PIC 9999    VALUE 0.

 

Notice that all of the fields above are grouped under the 01 level name INPUT-AREA. All of the names in WORKING-STORAGE meet the specification for making up names: letters, numbers, hyphen, no spaces and a maximum length of 32 characters.

Now we are going to look at the PROCEDURE DIVISION.

       PROCEDURE DIVISION.
       MAINLINE.
           DISPLAY "ENTER THE FIRST NUMBER (UNDER 1000)".
           ACCEPT FIRST-NUMBER.
           DISPLAY "ENTER THE SECOND NUMBER (UNDER 1000)".
           ACCEPT SECOND-NUMBER.
           ADD FIRST-NUMBER TO SECOND-NUMBER
               GIVING ADD-ANS.
           DISPLAY "THE ANSWER IS " ADD-ANS.
           DISPLAY "PRESS ENTER TO END THE PROGRAM".
           ACCEPT GET-ACCEPT-ANS.
           STOP RUN.

Remember that the PROCEDURE DIVISION is divided into paragraphs. There is only one paragraph in this program and it is called MAINLINE. This is a name that I made up.

This program uses 4 different commands or verbs: DISPLAY, ACCEPT, ADD, STOP RUN.

The first command is a DISPLAY, which displays the message asking the user to enter a number under 1000. Remember we decided we wanted a maximum of three digits. The second command accepts what the user keys in and stores it in the area in memory called FIRST-NUMBER. The third command asks the user to enter a second number. The number the user keys in is stored in memory under the name SECOND-NUMBER.

Now that the two numbers are stored in memory, we can add them together and store the answer in another area of memory that we defined in WORKING-STORAGE with the name ADD-ANS.

The ADD statement is:

ADD FIRST-NUMBER TO SECOND-NUMBER
    GIVING ADD-ANS.

In the ADD command, the word TO is optional but the word GIVING is required if I want to store the answer in a separate place. Traditionally the GIVING is shown on a second, indented line. This is not a requirement, just a convention! The format will be discussed in detail in later lessons.

Finally, after I have calculated the answer, I need to display it on the screen.

       PROCEDURE DIVISION.
       MAINLINE.
           DISPLAY "ENTER THE FIRST NUMBER (UNDER 1000)".
           ACCEPT FIRST-NUMBER.
           DISPLAY "ENTER THE SECOND NUMBER (UNDER 1000)".
           ACCEPT SECOND-NUMBER.
           ADD FIRST-NUMBER TO SECOND-NUMBER
               GIVING ADD-ANS.
           DISPLAY "THE ANSWER IS " ADD-ANS.
           DISPLAY "PRESS ENTER TO END THE PROGRAM".
           ACCEPT GET-ACCEPT-ANS.
           STOP RUN.

Notice that this time, I not only use the display to put out a message, I also have it display the contents of ADD-ANS.

        DISPLAY "THE ANSWER IS " ADD-ANS.

Next we display the message telling the user to press enter to end the program and accept the user answer into GET-ACCEPT-ANS. Remember when an ACCEPT is encountered, the program waits until the user has entered data and pressed ENTER. In the case of the numbers, they did both. Here they just need to press ENTER.

When the program continues, it executes the command STOP RUN and the program ends.