0% found this document useful (0 votes)
67 views18 pages

Cobol Coding Questions

The document contains various COBOL coding examples, including programs for reversing a string, submitting a COBOL program using JCL, fetching data from a DB2 table, reading records from VSAM using CICS, and performing searches and calculations on tables. Each example includes explanations, key points, and relevant code snippets. Additionally, it covers JCL scripts for file creation and data writing, as well as updating records in a DB2 table.

Uploaded by

srb120397
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views18 pages

Cobol Coding Questions

The document contains various COBOL coding examples, including programs for reversing a string, submitting a COBOL program using JCL, fetching data from a DB2 table, reading records from VSAM using CICS, and performing searches and calculations on tables. Each example includes explanations, key points, and relevant code snippets. Additionally, it covers JCL scripts for file creation and data writing, as well as updating records in a DB2 table.

Uploaded by

srb120397
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

COBOL CODING QUESTIONS –

1. COBOL - Program to Reverse a String

Explanation:
This program reverses a given string by iterating through it backward and storing the reversed
result in another variable.

cbol
CopyEdit
IDENTIFICATION DIVISION.
PROGRAM-ID. ReverseString.

DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-INPUT-STRING PIC X(50) VALUE 'HELLO WORLD'. * Input string to
reverse.
01 WS-OUTPUT-STRING PIC X(50). * Stores the reversed
string.
01 WS-LENGTH PIC 9(4) COMP. * Holds the length of
the input string.
01 WS-INDEX PIC 9(4) COMP. * Loop index variable.

PROCEDURE DIVISION.
MOVE FUNCTION LENGTH(WS-INPUT-STRING) TO WS-LENGTH * Get the length of
the string.
PERFORM VARYING WS-INDEX FROM 1 BY 1 UNTIL WS-INDEX > WS-LENGTH
MOVE WS-INPUT-STRING(WS-LENGTH - WS-INDEX + 1:1) * Pick characters
from the end.
TO WS-OUTPUT-STRING(WS-INDEX:1) * Store them in
reverse order.
END-PERFORM
DISPLAY 'REVERSED STRING: ' WS-OUTPUT-STRING * Display the
reversed string.
STOP RUN.

Key Points:

 The FUNCTION LENGTH is used to calculate the length of the string.


 Characters are accessed from the end of WS-INPUT-STRING using substring notation.
 The reversed string is built by assigning characters one by one into WS-OUTPUT-STRING.

2. JCL - Submit a COBOL Program

Explanation:
This JCL script compiles a COBOL program and executes it.
//JOBNAME JOB (ACCT),'DESCRIPTION',CLASS=A,MSGCLASS=A,NOTIFY=&SYSUID
//STEP1 EXEC PGM=IGYCRCTL * COBOL
compiler.
//SYSIN DD DSN=MY.COBOL.SOURCE(REVERSE),DISP=SHR * Input COBOL
source.
//SYSLIB DD DSN=MY.COBOL.COPYLIB,DISP=SHR * Copybook
library.
//SYSLMOD DD DSN=MY.COBOL.LOADLIB,DISP=SHR * Load module
output.
//SYSPRINT DD SYSOUT=* * Compilation
output.
//SYSUT1 DD UNIT=SYSDA,SPACE=(CYL,(1,1)) * Temporary
storage.
//*
//STEP2 EXEC PGM=REVERSE * Execute the
compiled program.
//STEPLIB DD DSN=MY.COBOL.LOADLIB,DISP=SHR * Path to load
module.
//SYSOUT DD SYSOUT=* * Program
output.
//SYSIN DD * * Input to the
program.
INPUT STRING: HELLO WORLD * Example
input.

Key Points:

 STEP1 compiles the COBOL source code into a load module.


 STEP2 executes the compiled program using the load module.

3. DB2 - Fetch Data from a Table

Explanation:
This program fetches employee details from a DB2 table using embedded SQL.

IDENTIFICATION DIVISION.
PROGRAM-ID. FetchEmployeeDetails.

DATA DIVISION.
WORKING-STORAGE SECTION.
EXEC SQL INCLUDE SQLCA END-EXEC. * SQL
communication area.
01 WS-EMP-ID PIC X(10). * Holds employee
ID.
01 WS-EMP-NAME PIC X(50). * Holds employee
name.
01 WS-EMP-SALARY PIC 9(9)V99. * Holds employee
salary.
01 WS-SQLCODE PIC S9(4) COMP. * Holds SQL
return code.
PROCEDURE DIVISION.
EXEC SQL
DECLARE EMPLOYEE_CURSOR CURSOR FOR * Declare cursor
to fetch rows.
SELECT EMP_ID, EMP_NAME, EMP_SALARY
FROM EMPLOYEE
END-EXEC.

EXEC SQL OPEN EMPLOYEE_CURSOR END-EXEC. * Open the cursor.


PERFORM UNTIL SQLCODE NOT = 0 * Loop until all
rows are fetched.
EXEC SQL
FETCH EMPLOYEE_CURSOR * Fetch data into
variables.
INTO :WS-EMP-ID, :WS-EMP-NAME, :WS-EMP-SALARY
END-EXEC
DISPLAY 'ID: ' WS-EMP-ID ' NAME: ' WS-EMP-NAME ' SALARY: ' WS-EMP-
SALARY
END-PERFORM.

EXEC SQL CLOSE EMPLOYEE_CURSOR END-EXEC. * Close the


cursor.
STOP RUN.

Key Points:

 Cursor is used to fetch multiple rows from a table.


 SQLCODE is checked after every SQL operation for error handling.
 Variables like WS-EMP-ID are populated with table data during the FETCH.

4. CICS - Read a Record from VSAM

Explanation:
This program reads a record from a VSAM file using CICS commands.

IDENTIFICATION DIVISION.
PROGRAM-ID. ReadVSAM.

DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-EMP-ID PIC X(10). * Key of the VSAM
record.
01 WS-EMP-DATA PIC X(100). * Data read from
VSAM file.
01 WS-RESP-CODE PIC S9(4) COMP. * CICS response
code.

FILE-CONTROL.
SELECT EMPFILE ASSIGN TO 'EMPLOYEE.VSAM'. * VSAM file name.

PROCEDURE DIVISION.
EXEC CICS READ
FILE('EMPFILE') * File name.
INTO(WS-EMP-DATA) * Buffer to hold
record data.
RIDFLD(WS-EMP-ID) * Key for record
search.
RESP(WS-RESP-CODE) * Response code.
END-EXEC.

IF WS-RESP-CODE = DFHRESP(NORMAL) * Check for


successful read.
DISPLAY 'EMPLOYEE DATA: ' WS-EMP-DATA
ELSE
DISPLAY 'ERROR READING VSAM FILE: ' WS-RESP-CODE.

STOP RUN.

Key Points:

 CICS READ command fetches a record based on the key (RIDFLD).


 The RESP field holds the response code to determine if the read was successful.

5. COBOL - Search in a Table (Sequential Search)

Explanation:
This program searches for a specific value in a COBOL table using a sequential search.

IDENTIFICATION DIVISION.
PROGRAM-ID. SearchTable.

DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-TABLE.
05 WS-ELEMENT OCCURS 10 TIMES INDEXED BY IDX. * Table with 10
elements.
10 WS-VALUE PIC X(10). * Value of each
element.
01 WS-SEARCH-VALUE PIC X(10) VALUE 'VALUE05'. * Value to search.

PROCEDURE DIVISION.
MOVE 'VALUE01' TO WS-VALUE(1) * Initialize table
elements.
MOVE 'VALUE02' TO WS-VALUE(2)
MOVE 'VALUE05' TO WS-VALUE(3)
SET IDX TO 1 * Start index at 1.
PERFORM UNTIL IDX > 10 OR WS-VALUE(IDX) = WS-SEARCH-VALUE
SET IDX UP BY 1 * Increment index.
END-PERFORM

IF WS-VALUE(IDX) = WS-SEARCH-VALUE * Check if value is


found.
DISPLAY 'VALUE FOUND AT POSITION: ' IDX
ELSE
DISPLAY 'VALUE NOT FOUND'.

STOP RUN.

Key Points:

 OCCURS defines the table structure with 10 elements.


 Sequential search checks each element until a match is found.
 Indexed by IDX, the search stops when the value is found or the end of the table is
reached.

6. COBOL - Find Maximum in a Table

Question: Write a COBOL program to find the maximum value in a table.

IDENTIFICATION DIVISION.
PROGRAM-ID. FindMaximum.

DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-TABLE.
05 WS-ELEMENT OCCURS 10 TIMES INDEXED BY IDX. * Table of 10
elements.
10 WS-VALUE PIC 9(3). * Numeric value of
each element.
01 WS-MAX-VALUE PIC 9(3) VALUE 0. * Stores the maximum
value.
01 WS-CURRENT-VALUE PIC 9(3). * Holds current value
during iteration.

PROCEDURE DIVISION.
MOVE 10 TO WS-VALUE(1) * Initialize table
values.
MOVE 20 TO WS-VALUE(2)
MOVE 50 TO WS-VALUE(3)
MOVE 30 TO WS-VALUE(4)
MOVE 40 TO WS-VALUE(5)

SET IDX TO 1 * Start index at 1.


PERFORM UNTIL IDX > 10
MOVE WS-VALUE(IDX) TO WS-CURRENT-VALUE * Fetch the current
value.
IF WS-CURRENT-VALUE > WS-MAX-VALUE
MOVE WS-CURRENT-VALUE TO WS-MAX-VALUE * Update maximum if
current is larger.
END-IF
SET IDX UP BY 1 * Move to the next
element.
END-PERFORM

DISPLAY 'MAXIMUM VALUE: ' WS-MAX-VALUE * Display the maximum


value.
STOP RUN.

Key Points:

 Iterates over the table using IDX.


 Compares each value with WS-MAX-VALUE and updates it if the current value is greater.
 Displays the final maximum value.

7. JCL - Create and Write to a Sequential File

Question: Write a JCL script to create a sequential file and write data into it.

jcl
CopyEdit
//JOBNAME JOB (ACCT),'CREATE FILE',CLASS=A,MSGCLASS=A,NOTIFY=&SYSUID
//STEP1 EXEC PGM=IEFBR14 * Utility to create
files.
//DD1 DD DSN=MY.DATA.FILE, * Define the new
file.
// DISP=(NEW,CATLG,DELETE), * New file; catalog
it.
// SPACE=(CYL,(1,1)), * Allocate space.
// DCB=(RECFM=FB,LRECL=80,BLKSIZE=800) * File attributes.
//*
//STEP2 EXEC PGM=IEBGENER * Write data to the
file.
//SYSUT1 DD * * Input data.
//NAME: JOHN DOE
//AGE: 30
//LOCATION: NEW YORK
//SYSUT2 DD DSN=MY.DATA.FILE,DISP=SHR * Output file.
//SYSPRINT DD SYSOUT=* * Print output.
//SYSIN DD DUMMY * No control
statements.

Key Points:

 IEFBR14 is used to create the file.


 IEBGENER is used to copy data into the created file.
 File attributes like LRECL (record length) and RECFM (record format) are specified.

8. DB2 - Update a Record in a Table

Question: Write a COBOL-DB2 program to update an employee's salary in the EMPLOYEE table.

IDENTIFICATION DIVISION.
PROGRAM-ID. UpdateSalary.

DATA DIVISION.
WORKING-STORAGE SECTION.
EXEC SQL INCLUDE SQLCA END-EXEC. * SQL communication
area.
01 WS-EMP-ID PIC X(10) VALUE 'E001'. * Employee ID to
update.
01 WS-NEW-SALARY PIC 9(9)V99 VALUE 75000. * New salary to set.
01 WS-SQLCODE PIC S9(4) COMP. * SQL return code.

PROCEDURE DIVISION.
EXEC SQL
UPDATE EMPLOYEE
SET EMP_SALARY = :WS-NEW-SALARY * Update salary
field.
WHERE EMP_ID = :WS-EMP-ID * Match the given ID.
END-EXEC.

IF SQLCODE = 0 * Check for success.


DISPLAY 'SALARY UPDATED SUCCESSFULLY'
ELSE
DISPLAY 'ERROR UPDATING SALARY: ' SQLCODE. * Display error code.

STOP RUN.

Key Points:

 The UPDATE SQL statement modifies the EMP_SALARY column.


 SQLCODE is checked for success (0) or failure (non-zero).

9. COBOL - Sort an Array

Question: Write a COBOL program to sort an array of numbers in ascending order.

IDENTIFICATION DIVISION.
PROGRAM-ID. SortArray.

DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-TABLE.
05 WS-NUMBER OCCURS 5 TIMES INDEXED BY IDX1 IDX2. * Array of 5 numbers.
10 WS-VALUE PIC 9(3). * Each number is 3
digits.
01 WS-TEMP PIC 9(3). * Temporary storage
for swapping.

PROCEDURE DIVISION.
MOVE 50 TO WS-NUMBER(1) * Initialize array.
MOVE 20 TO WS-NUMBER(2)
MOVE 40 TO WS-NUMBER(3)
MOVE 10 TO WS-NUMBER(4)
MOVE 30 TO WS-NUMBER(5)

PERFORM VARYING IDX1 FROM 1 BY 1 UNTIL IDX1 = 5 * Outer loop.


PERFORM VARYING IDX2 FROM IDX1 + 1 BY 1 UNTIL IDX2 > 5
IF WS-NUMBER(IDX1) > WS-NUMBER(IDX2) * Compare adjacent
values.
MOVE WS-NUMBER(IDX1) TO WS-TEMP * Swap if out of
order.
MOVE WS-NUMBER(IDX2) TO WS-NUMBER(IDX1)
MOVE WS-TEMP TO WS-NUMBER(IDX2)
END-IF
END-PERFORM
END-PERFORM

DISPLAY 'SORTED ARRAY: ' * Display sorted


array.
PERFORM VARYING IDX1 FROM 1 BY 1 UNTIL IDX1 > 5
DISPLAY WS-NUMBER(IDX1)
END-PERFORM.

STOP RUN.

Key Points:

 Implements bubble sort to arrange numbers in ascending order.


 Nested loops are used for comparisons and swaps.

1. Daily Interest Calculation for Savings Accounts


IDENTIFICATION DIVISION.
PROGRAM-ID. DailyInterestCalculation.

DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-INTEREST-RATE PIC 9(3)V99 VALUE 0.02. "Annual interest
rate (2%)."
01 WS-DAYS-IN-YEAR PIC 9(3) VALUE 365. "Number of days in a
year, used for daily interest calculation."
01 WS-ACCOUNT-ID PIC X(10). "Holds the account
ID fetched from the database."
01 WS-ACCOUNT-BALANCE PIC 9(9)V99. "Stores the current
account balance."
01 WS-DAILY-INTEREST PIC 9(9)V99. "Holds the
calculated daily interest for an account."

EXEC SQL INCLUDE SQLCA END-EXEC. "Includes SQL


Communication Area for DB2 operations."

PROCEDURE DIVISION.
DISPLAY 'STARTING DAILY INTEREST CALCULATION' "Logs the start of
the process."
EXEC SQL
DECLARE ACCOUNTS CURSOR FOR
SELECT ACCOUNT_ID, BALANCE
FROM SAVINGS_ACCOUNTS
WHERE STATUS = 'ACTIVE' "Fetches only active
savings accounts."
END-EXEC.

EXEC SQL OPEN ACCOUNTS END-EXEC. "Opens the cursor to


begin fetching records."

PERFORM UNTIL SQLCODE <> 0 "Loop until no more


records are fetched (SQLCODE = 100)."
EXEC SQL FETCH ACCOUNTS
INTO :WS-ACCOUNT-ID, :WS-ACCOUNT-BALANCE "Fetches account
details into working storage."
END-EXEC.

IF SQLCODE = 0 "Processes the


record if fetch is successful."
COMPUTE WS-DAILY-INTEREST =
(WS-ACCOUNT-BALANCE * WS-INTEREST-RATE) / WS-DAYS-IN-YEAR
"Calculates daily interest based on the formula: (Balance *
Rate) / 365."

COMPUTE WS-ACCOUNT-BALANCE =
WS-ACCOUNT-BALANCE + WS-DAILY-INTEREST "Adds daily interest
to the account balance."

EXEC SQL
UPDATE SAVINGS_ACCOUNTS
SET BALANCE = :WS-ACCOUNT-BALANCE
WHERE ACCOUNT_ID = :WS-ACCOUNT-ID "Updates the balance
in the database."
END-EXEC.
END-IF
END-PERFORM.

EXEC SQL CLOSE ACCOUNTS END-EXEC. "Closes the cursor


after processing all accounts."

DISPLAY 'DAILY INTEREST CALCULATION COMPLETED' "Logs the completion


of the process."
STOP RUN.

Explanation:

1. Purpose: Automates daily interest calculations for all active savings accounts.
2. Key Operations:
o Cursor fetches account details from the SAVINGS_ACCOUNTS table.
o Interest is calculated using the formula (Balance * Interest Rate) / 365.
o Updated balances are stored back in the database.
3. SQLCODE: Monitors successful fetches (SQLCODE = 0) and determines the end of data
(SQLCODE = 100).
2. Batch File Processing for Bulk Payments
IDENTIFICATION DIVISION.
PROGRAM-ID. ProcessBulkPayments.

DATA DIVISION.
FILE SECTION.
FD INPUT-FILE.
01 INPUT-RECORD.
05 ACCOUNT-ID PIC X(10). "Holds the account
ID from the input file."
05 PAYMENT-AMOUNT PIC 9(9)V99. "Holds the payment
amount from the input file."

WORKING-STORAGE SECTION.
01 WS-ACCOUNT-ID PIC X(10). "Stores the current
account ID."
01 WS-PAYMENT-AMOUNT PIC 9(9)V99. "Stores the current
payment amount."
01 WS-ACCOUNT-BALANCE PIC 9(9)V99. "Holds the fetched
balance of the current account."

PROCEDURE DIVISION.
OPEN INPUT INPUT-FILE "Opens the input
file for reading."
DISPLAY 'PROCESSING BULK PAYMENTS'

READ INPUT-FILE INTO INPUT-RECORD "Reads the first


record from the input file."
PERFORM UNTIL EOF "Loops through the
file until end-of-file."
MOVE ACCOUNT-ID TO WS-ACCOUNT-ID "Moves the input
account ID to working storage."
MOVE PAYMENT-AMOUNT TO WS-PAYMENT-AMOUNT "Moves the input
payment amount to working storage."

EXEC SQL
SELECT BALANCE
INTO :WS-ACCOUNT-BALANCE
FROM ACCOUNTS
WHERE ACCOUNT_ID = :WS-ACCOUNT-ID "Fetches the current
account balance."
END-EXEC

IF SQLCODE = 0 "Checks if the


account exists."
COMPUTE WS-ACCOUNT-BALANCE =
WS-ACCOUNT-BALANCE - WS-PAYMENT-AMOUNT "Deducts the payment
amount from the balance."

EXEC SQL
UPDATE ACCOUNTS
SET BALANCE = :WS-ACCOUNT-BALANCE
WHERE ACCOUNT_ID = :WS-ACCOUNT-ID "Updates the balance
in the database."
END-EXEC
ELSE
DISPLAY 'ACCOUNT NOT FOUND: ' WS-ACCOUNT-ID "Logs error if the
account is missing."
END-IF

READ INPUT-FILE INTO INPUT-RECORD "Reads the next


record."
END-PERFORM.

CLOSE INPUT-FILE "Closes the input


file."
DISPLAY 'BULK PAYMENT PROCESSING COMPLETED'
STOP RUN.

Explanation:

1. Purpose: Processes payment instructions from a file and updates account balances.
2. Key Operations:
o Reads payment details (account ID and amount) from an input file.
o Deducts payment from the current balance fetched from the ACCOUNTS table.
o Handles errors for invalid accounts.
3. Efficiency: Uses SQL for direct database interaction, minimizing processing time.

3. Transaction Reconciliation
IDENTIFICATION DIVISION.
PROGRAM-ID. TransactionReconciliation.

DATA DIVISION.
FILE SECTION.
FD SYSTEM1-FILE.
01 SYS1-RECORD.
05 SYS1-TRANSACTION-ID PIC X(10). "Transaction ID from
System 1."
05 SYS1-AMOUNT PIC 9(9)V99. "Transaction amount
from System 1."

FD SYSTEM2-FILE.
01 SYS2-RECORD.
05 SYS2-TRANSACTION-ID PIC X(10). "Transaction ID from
System 2."
05 SYS2-AMOUNT PIC 9(9)V99. "Transaction amount
from System 2."

WORKING-STORAGE SECTION.
01 WS-SYS1-TRANSACTION-ID PIC X(10). "Stores System 1
transaction ID."
01 WS-SYS2-TRANSACTION-ID PIC X(10). "Stores System 2
transaction ID."
01 WS-SYS1-AMOUNT PIC 9(9)V99. "Stores System 1
transaction amount."
01 WS-SYS2-AMOUNT PIC 9(9)V99. "Stores System 2
transaction amount."

PROCEDURE DIVISION.
OPEN INPUT SYSTEM1-FILE SYSTEM2-FILE "Opens both files
for reading."
DISPLAY 'STARTING TRANSACTION RECONCILIATION'

READ SYSTEM1-FILE INTO SYS1-RECORD "Reads the first


record from System 1."
READ SYSTEM2-FILE INTO SYS2-RECORD "Reads the first
record from System 2."

PERFORM UNTIL EOF-SYSTEM1 OR EOF-SYSTEM2 "Continues until


either file reaches EOF."
IF SYS1-TRANSACTION-ID = SYS2-TRANSACTION-ID "Checks if
transaction IDs match."
IF SYS1-AMOUNT = SYS2-AMOUNT
DISPLAY 'MATCHED: ' SYS1-TRANSACTION-ID "Logs matching
transactions."
ELSE
DISPLAY 'MISMATCHED AMOUNT: ' SYS1-TRANSACTION-ID
END-IF
READ SYSTEM1-FILE INTO SYS1-RECORD "Reads the next
record from both files."
READ SYSTEM2-FILE INTO SYS2-RECORD
ELSE IF SYS1-TRANSACTION-ID < SYS2-TRANSACTION-ID
DISPLAY 'MISSING IN SYSTEM2: ' SYS1-TRANSACTION-ID
READ SYSTEM1-FILE INTO SYS1-RECORD "Reads the next
record from System 1."
ELSE
DISPLAY 'MISSING IN SYSTEM1: ' SYS2-TRANSACTION-ID
READ SYSTEM2-FILE INTO SYS2-RECORD "Reads the next
record from System 2."
END-IF
END-PERFORM.

CLOSE SYSTEM1-FILE SYSTEM2-FILE "Closes both files."


DISPLAY 'TRANSACTION RECONCILIATION COMPLETED'
STOP RUN.

Explanation:

1. Purpose: Reconciles transactions between two systems to ensure data consistency.


2. Key Operations:
o Compares records in two sorted transaction files.
o Identifies mismatched or missing transactions.

1. Card Expiration Validation

Scenario: Validate if a card is active by checking its expiration date.


IDENTIFICATION DIVISION.
PROGRAM-ID. CardExpirationValidation.

DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-CARD-NUMBER PIC X(16). "Stores the card
number."
01 WS-EXPIRY-DATE PIC 9(6). "Stores the card
expiry date in YYYYMM format."
01 WS-CURRENT-DATE PIC 9(6). "Stores the current
date in YYYYMM format."
01 WS-VALID-FLAG PIC X(1). "Holds 'Y' for valid,
'N' for expired."

PROCEDURE DIVISION.
DISPLAY 'START CARD EXPIRATION VALIDATION.'

ACCEPT WS-CURRENT-DATE FROM DATE YYYYMM "Fetches the system


date in YYYYMM format."

PERFORM WITH TEST AFTER UNTIL WS-CARD-NUMBER = SPACES


DISPLAY 'ENTER CARD NUMBER (16 DIGITS):'
ACCEPT WS-CARD-NUMBER "Accepts card number
input."

DISPLAY 'ENTER CARD EXPIRY DATE (YYYYMM):'


ACCEPT WS-EXPIRY-DATE "Accepts expiry date
input."

IF WS-EXPIRY-DATE >= WS-CURRENT-DATE


MOVE 'Y' TO WS-VALID-FLAG
DISPLAY 'CARD IS VALID.'
ELSE
MOVE 'N' TO WS-VALID-FLAG
DISPLAY 'CARD IS EXPIRED.'
END-IF

DISPLAY 'CHECK NEXT CARD? ENTER SPACE TO STOP, ELSE ENTER ANY KEY:'
ACCEPT WS-CARD-NUMBER
END-PERFORM.

DISPLAY 'CARD EXPIRATION VALIDATION COMPLETED.'


STOP RUN.

Explanation:

1. Purpose: Ensures a card is valid by checking its expiry date against the current system date.
2. Key Operations:
o System date is fetched using ACCEPT ... FROM DATE.
o Compares the expiry date with the current date (YYYYMM format).
o Outputs whether the card is valid or expired.
3. Practical Use: Commonly used in authorization systems to reject expired cards.
2. Credit Card Transaction Authorization

Scenario: Validate and authorize a credit card transaction based on available credit limit.

IDENTIFICATION DIVISION.
PROGRAM-ID. TransactionAuthorization.

DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-CARD-NUMBER PIC X(16). "Stores the card
number."
01 WS-TRANSACTION-AMOUNT PIC 9(7)V99. "Stores the
transaction amount."
01 WS-CREDIT-LIMIT PIC 9(9)V99. "Holds the credit
limit of the card."
01 WS-AVAILABLE-CREDIT PIC 9(9)V99. "Holds the available
credit after processing."
01 WS-STATUS-FLAG PIC X(1). "Holds 'A' for
approved, 'D' for declined."

PROCEDURE DIVISION.
DISPLAY 'START CREDIT CARD TRANSACTION AUTHORIZATION.'

DISPLAY 'ENTER CARD NUMBER (16 DIGITS):'


ACCEPT WS-CARD-NUMBER "Accepts the card
number."

DISPLAY 'ENTER CREDIT LIMIT:'


ACCEPT WS-CREDIT-LIMIT "Accepts the card's
credit limit."

DISPLAY 'ENTER TRANSACTION AMOUNT:'


ACCEPT WS-TRANSACTION-AMOUNT "Accepts the
transaction amount."

COMPUTE WS-AVAILABLE-CREDIT = WS-CREDIT-LIMIT - WS-TRANSACTION-AMOUNT


"Calculates the remaining credit after the transaction."

IF WS-AVAILABLE-CREDIT >= 0
MOVE 'A' TO WS-STATUS-FLAG
DISPLAY 'TRANSACTION APPROVED. REMAINING CREDIT: ' WS-AVAILABLE-CREDIT
ELSE
MOVE 'D' TO WS-STATUS-FLAG
DISPLAY 'TRANSACTION DECLINED. INSUFFICIENT CREDIT.'
END-IF

DISPLAY 'TRANSACTION AUTHORIZATION COMPLETED.'


STOP RUN.

Explanation:

1. Purpose: Authorizes or declines a transaction based on available credit.


2. Key Operations:
o Accepts card details, credit limit, and transaction amount.
o Computes the remaining credit (Credit Limit - Transaction Amount).
o Declines the transaction if the credit limit is exceeded.
3. Practical Use: Core logic for real-time transaction approval systems in payment gateways.

3. Fraudulent Transaction Detection

Scenario: Detect potential fraud by identifying transactions exceeding a predefined threshold.

IDENTIFICATION DIVISION.
PROGRAM-ID. FraudDetection.

DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-CARD-NUMBER PIC X(16). "Stores the card
number."
01 WS-TRANSACTION-AMOUNT PIC 9(7)V99. "Stores the
transaction amount."
01 WS-THRESHOLD-AMOUNT PIC 9(7)V99 VALUE 5000.00. "Predefined fraud
detection threshold."

PROCEDURE DIVISION.
DISPLAY 'START FRAUDULENT TRANSACTION DETECTION.'

DISPLAY 'ENTER CARD NUMBER (16 DIGITS):'


ACCEPT WS-CARD-NUMBER "Accepts the card
number."

DISPLAY 'ENTER TRANSACTION AMOUNT:'


ACCEPT WS-TRANSACTION-AMOUNT "Accepts the
transaction amount."

IF WS-TRANSACTION-AMOUNT > WS-THRESHOLD-AMOUNT


DISPLAY 'POTENTIAL FRAUD DETECTED ON CARD: ' WS-CARD-NUMBER
DISPLAY 'TRANSACTION AMOUNT: ' WS-TRANSACTION-AMOUNT
ELSE
DISPLAY 'TRANSACTION IS WITHIN THRESHOLD.'
END-IF

DISPLAY 'FRAUD DETECTION PROCESS COMPLETED.'


STOP RUN.

Explanation:

1. Purpose: Flags transactions that exceed a threshold, indicating possible fraud.


2. Key Operations:
o Compares the transaction amount with a predefined limit.
o Alerts for potential fraud if the limit is breached.
3. Practical Use: Part of risk management systems in card transaction monitoring.
4. Daily Settlement of Card Transactions

Scenario: Calculate the total debits and credits for the day and update the settlement file.

IDENTIFICATION DIVISION.
PROGRAM-ID. DailySettlement.

DATA DIVISION.
FILE SECTION.
FD TRANSACTION-FILE.
01 TRANSACTION-RECORD.
05 CARD-NUMBER PIC X(16). "Stores the card
number."
05 TRANSACTION-TYPE PIC X(1). "D = Debit, C =
Credit."
05 TRANSACTION-AMOUNT PIC 9(7)V99. "Transaction amount."

WORKING-STORAGE SECTION.
01 WS-TOTAL-DEBITS PIC 9(9)V99 VALUE 0.00. "Total amount of
debits for the day."
01 WS-TOTAL-CREDITS PIC 9(9)V99 VALUE 0.00. "Total amount of
credits for the day."

PROCEDURE DIVISION.
OPEN INPUT TRANSACTION-FILE "Opens the
transaction file for reading."
DISPLAY 'START DAILY SETTLEMENT PROCESS.'

READ TRANSACTION-FILE INTO TRANSACTION-RECORD


PERFORM UNTIL EOF "Processes all
records in the file."
IF TRANSACTION-TYPE = 'D'
ADD TRANSACTION-AMOUNT TO WS-TOTAL-DEBITS "Accumulates debit
amounts."
ELSE IF TRANSACTION-TYPE = 'C'
ADD TRANSACTION-AMOUNT TO WS-TOTAL-CREDITS "Accumulates credit
amounts."
END-IF

READ TRANSACTION-FILE INTO TRANSACTION-RECORD "Reads the next


record."
END-PERFORM.

CLOSE TRANSACTION-FILE "Closes the


transaction file."

DISPLAY 'TOTAL DEBITS: ' WS-TOTAL-DEBITS


DISPLAY 'TOTAL CREDITS: ' WS-TOTAL-CREDITS
DISPLAY 'DAILY SETTLEMENT PROCESS COMPLETED.'
STOP RUN.

Explanation:

1. Purpose: Calculates the total debit and credit transactions for a day.
2. Key Operations:
o Reads transactions from a file.
o Segregates and sums up debits and credits.
o Outputs the totals for settlement purposes.
3. Practical Use: Forms part of the reconciliation and settlement process in card systems.

5. PIN Change Processing

Scenario: Update the PIN for a card after verifying the existing PIN.

IDENTIFICATION DIVISION.
PROGRAM-ID. PINChange.

DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-CARD-NUMBER PIC X(16). "Stores the card
number."
01 WS-OLD-PIN PIC X(4). "Stores the existing
PIN entered by the user."
01 WS-NEW-PIN PIC X(4). "Stores the new PIN
entered by the user."
01 WS-STORED-PIN PIC X(4). "Holds the PIN
fetched from the database."

PROCEDURE DIVISION.
DISPLAY 'START PIN CHANGE PROCESS.'

DISPLAY 'ENTER CARD NUMBER (16 DIGITS):'


ACCEPT WS-CARD-NUMBER "Accepts the card
number."

DISPLAY 'ENTER EXISTING PIN:'


ACCEPT WS-OLD-PIN "Accepts the old
PIN."

EXEC SQL
SELECT PIN INTO :WS-STORED-PIN
FROM CARD_ACCOUNTS
WHERE CARD_NUMBER = :WS-CARD-NUMBER "Fetches the stored
PIN from the database."
END-EXEC.

IF WS-OLD-PIN = WS-STORED-PIN
DISPLAY 'ENTER NEW PIN:'
ACCEPT WS-NEW-PIN "Accepts the new
PIN."

EXEC SQL
UPDATE CARD_ACCOUNTS
SET PIN = :WS-NEW-PIN
WHERE CARD_NUMBER = :WS-CARD-NUMBER "Updates the PIN in
the database."
END-EXEC
DISPLAY 'PIN CHANGE SUCCESSFUL.'
ELSE
DISPLAY 'INVALID EXISTING PIN.'
END-IF

DISPLAY 'PIN CHANGE PROCESS COMPLETED.'


STOP RUN.

Explanation:

1. Purpose: Securely updates the PIN for a card after user authentication.
2. Key Operations:
o Validates the existing PIN.
o Updates the database with the new PIN if validation succeeds.
3. Practical Use: Used in ATM or online banking systems for secure PIN management.

You might also like