Day 4: Accepting User Input and Performing Arithmetic Operations in COBOL Programs for Real-World Applications

 

1. Introduction: Making COBOL Interactive

Until now, our COBOL programs have been static — displaying predefined messages or values. But to make programs dynamic and useful, we must allow user input. COBOL provides the ACCEPT statement to take input directly from the keyboard during program execution. This capability lets us build interactive systems like payroll entry, billing systems, or report generators that respond to user-provided data.

For instance, instead of hardcoding a value, you can prompt the user:

ACCEPT STUDENT-NAME.

This reads the input and stores it in the variable STUDENT-NAME. Such simplicity is one reason COBOL remains a favorite for data-driven business applications.


2. Performing Arithmetic Operations in COBOL

COBOL supports basic arithmetic operations such as ADD, SUBTRACT, MULTIPLY, DIVIDE, and COMPUTE. These statements allow calculations on numeric variables, similar to how modern languages use arithmetic operators.

Example:

ADD 5 TO TOTAL. SUBTRACT TAX FROM SALARY. MULTIPLY PRICE BY QUANTITY GIVING TOTAL. DIVIDE TOTAL BY COUNT GIVING AVERAGE.

The COMPUTE statement is the most flexible, as it supports complex expressions:

COMPUTE TOTAL = PRICE * QUANTITY + TAX.

These operations are especially useful in payroll, accounting, and inventory systems — the core of COBOL’s business domain.


3. Combining Input and Arithmetic in One Program

Let’s combine what you’ve learned to make a simple calculator-like program. This program accepts two numbers and performs an addition:

IDENTIFICATION DIVISION. PROGRAM-ID. ADDITION-DEMO. DATA DIVISION. WORKING-STORAGE SECTION. 01 NUM1 PIC 9(3). 01 NUM2 PIC 9(3). 01 RESULT PIC 9(4). PROCEDURE DIVISION. DISPLAY "ENTER FIRST NUMBER: ". ACCEPT NUM1. DISPLAY "ENTER SECOND NUMBER: ". ACCEPT NUM2. ADD NUM1 TO NUM2 GIVING RESULT. DISPLAY "THE SUM IS: " RESULT. STOP RUN.

This demonstrates how COBOL handles both user input and arithmetic seamlessly, reflecting real business logic execution.


🧠 Key Takeaways

  • ACCEPT is used to take input from the user at runtime.

  • COBOL arithmetic includes ADD, SUBTRACT, MULTIPLY, DIVIDE, and COMPUTE.

  • You can combine these to create interactive business programs that calculate, process, and display results instantly.


💻 Practice Task

  1. Create a new COBOL program named SalaryCalculator.cbl.

  2. Accept basic employee details — name, base salary, and bonus.

  3. Calculate total salary using the COMPUTE statement.

  4. Display the result in a clear, formatted way.

Example:

IDENTIFICATION DIVISION. PROGRAM-ID. SALARY-CALCULATOR. DATA DIVISION. WORKING-STORAGE SECTION. 01 EMP-NAME PIC X(25). 01 BASE-SALARY PIC 9(6)V99. 01 BONUS PIC 9(5)V99. 01 TOTAL-SALARY PIC 9(7)V99. PROCEDURE DIVISION. DISPLAY "ENTER EMPLOYEE NAME: ". ACCEPT EMP-NAME. DISPLAY "ENTER BASE SALARY: ". ACCEPT BASE-SALARY. DISPLAY "ENTER BONUS: ". ACCEPT BONUS. COMPUTE TOTAL-SALARY = BASE-SALARY + BONUS. DISPLAY "EMPLOYEE: " EMP-NAME. DISPLAY "TOTAL SALARY: " TOTAL-SALARY. STOP RUN.

"This Content Sponsored by SBO Digital Marketing.

Mobile-Based Part-Time Job Opportunity by SBO!

Earn money online by doing simple content publishing and sharing tasks. Here's how:

  • Job Type: Mobile-based part-time work
  • Work Involves:
    • Content publishing
    • Content sharing on social media
  • Time Required: As little as 1 hour a day
  • Earnings: ₹300 or more daily
  • Requirements:
    • Active Facebook and Instagram account
    • Basic knowledge of using mobile and social media

For more details:

WhatsApp your Name and Qualification to 9994104160

a.Online Part Time Jobs from Home

b.Work from Home Jobs Without Investment

c.Freelance Jobs Online for Students

d.Mobile Based Online Jobs

e.Daily Payment Online Jobs

Keyword & Tag: #OnlinePartTimeJob #WorkFromHome #EarnMoneyOnline #PartTimeJob #jobs #jobalerts #withoutinvestmentjob"



Comments