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:
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:
The COMPUTE statement is the most flexible, as it supports complex expressions:
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:
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
-
Create a new COBOL program named
SalaryCalculator.cbl. -
Accept basic employee details — name, base salary, and bonus.
-
Calculate total salary using the
COMPUTEstatement. -
Display the result in a clear, formatted way.
Example:
.png)
Comments
Post a Comment