Skip to content

Instantly share code, notes, and snippets.

@sandeep-sparrow
Last active September 30, 2020 06:48
Show Gist options
  • Save sandeep-sparrow/7a3593ac465df1394d56d148430105cd to your computer and use it in GitHub Desktop.
Save sandeep-sparrow/7a3593ac465df1394d56d148430105cd to your computer and use it in GitHub Desktop.
SEARCH function in COBOL.
IDENTIFICATION DIVISION.
PROGRAM-ID. CBLTAXS.
*
ENVIRONMENT DIVISION.
*
DATA DIVISION.
*
WORKING-STORAGE SECTION.
*
01 TAX-SLAB.
05 FILLER PIC X(20) VALUE '02500010005050000000'.
05 FILLER PIC X(20) VALUE '05000010010075000000'.
05 FILLER PIC X(20) VALUE '07500010015100000000'.
05 FILLER PIC X(20) VALUE '10000010020125000000'.
05 FILLER PIC X(20) VALUE '12500010025150000000'.
*
01 TAX-SLAB-TABLE REDEFINES TAX-SLAB.
05 TAX-SLAB-ITEM OCCURS 5 TIMES INDEXED BY TX-IDX.
10 TAX-SLAB-L-RANGE PIC 9(07)V9(2).
10 TAX-SLAB-RATE PIC 9(2).
10 TAX-SLAB-H-RANGE PIC 9(07)V9(2).
*
77 IN-ITEM-TAX-AMT PIC 9(07)V9(2).
77 TAX-AMT PIC 9(07)V9(2).
77 FINAL-AMT PIC 9(07)V9(2).
77 IN-ITEM-TAX-AMT-E PIC ZZ,ZZ,ZZ9.99.
77 TAX-AMT-E PIC ZZ,ZZ,ZZ9.99.
77 FINAL-AMT-E PIC ZZ,ZZ,ZZ9.99.
77 TABLE-MAX PIC 9(4) COMP.
*
PROCEDURE DIVISION.
*
COMPUTE TABLE-MAX = FUNCTION LENGTH(TAX-SLAB-TABLE) /
FUNCTION LENGTH(TAX-SLAB-ITEM(1)).
*
DISPLAY "Table Max: " TABLE-MAX.
*
ACCEPT IN-ITEM-TAX-AMT.
*
SET TX-IDX TO 1.
*
SEARCH TAX-SLAB-ITEM
AT END
DISPLAY "You don't fall under any tax Slab"
WHEN TAX-SLAB-H-RANGE(TX-IDX) > IN-ITEM-TAX-AMT AND
TAX-SLAB-L-RANGE(TX-IDX) < IN-ITEM-TAX-AMT
PERFORM 100-COMPUTE-TAX
END-SEARCH.
*
STOP RUN.
*
100-COMPUTE-TAX.
*
COMPUTE TAX-AMT = IN-ITEM-TAX-AMT *
TAX-SLAB-RATE(TX-IDX) / 100.
*
COMPUTE FINAL-AMT = IN-ITEM-TAX-AMT - TAX-AMT.
*
MOVE TAX-AMT TO TAX-AMT-E.
MOVE FINAL-AMT TO FINAL-AMT-E.
MOVE IN-ITEM-TAX-AMT TO IN-ITEM-TAX-AMT-E.
*
DISPLAY "Tax rate : "
TAX-SLAB-RATE(TX-IDX) "%".
DISPLAY "Your initial amount before tax is: "
IN-ITEM-TAX-AMT-E.
DISPLAY "Total amount of tax deducted : " TAX-AMT-E.
DISPLAY "Your final amount after tax is : " FINAL-AMT-E.
*
Linear search or Binary Search...
Sequential SEARCH ->
SET IDX TO 1
SEARCH table-name varying IDX
AT END
WHEN condition or condition / condition and condition
END-SEARCH
Binary SEARCH ALL ->
SEARCH ALL table
AT END
WHEN condition or condition / condition and condition
END-SEARCH
compiler requires the table to be indexed...or else there is syntax error...
you can redefines a bunch of values as a table as long the tabel values are fix and match up with picture clause.
output1:
Table Max: 0005
Tax rate : 20%
Your initial amount before tax is: 10,95,000.25
Total amount of tax deducted : 2,19,000.05
Your final amount after tax is : 8,76,000.20
output2:
Table Max: 0005
Tax rate : 25%
Your initial amount before tax is: 13,95,000.25
Total amount of tax deducted : 3,48,750.06
Your final amount after tax is : 10,46,250.19
output3:
Table Max: 0005
Tax rate : 15%
Your initial amount before tax is: 8,95,000.25
Total amount of tax deducted : 1,34,250.03
Your final amount after tax is : 7,60,750.22
output4:
Table Max: 0005
Tax rate : 10%
Your initial amount before tax is: 6,95,000.25
Total amount of tax deducted : 69,500.02
Your final amount after tax is : 6,25,500.23
output5:
Table Max: 0005
Tax rate : 05%
Your initial amount before tax is: 3,95,000.25
Total amount of tax deducted : 19,750.01
Your final amount after tax is : 3,75,250.24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment