Skip to content

Instantly share code, notes, and snippets.

@AccordionGuy
Last active April 17, 2020 18:26
Show Gist options
  • Save AccordionGuy/bfc11d7965b10a0380c747298d314ca0 to your computer and use it in GitHub Desktop.
Save AccordionGuy/bfc11d7965b10a0380c747298d314ca0 to your computer and use it in GitHub Desktop.
******************************************************************
*
* Stupid Interest Calculator
* ==========================
*
* A sample COBOL app to demonstrate the programming language
* and make me doubt that I’m living in the 21st century.
*
******************************************************************
IDENTIFICATION DIVISION.
PROGRAM-ID. STUPID-INTEREST-CALCULATOR.
DATA DIVISION.
FILE SECTION.
WORKING-STORAGE SECTION.
* In COBOL, you declare variables in the WORKING-STORAGE section.
* Let’s declare a string variable for the user’s name.
* The string will be 20 characters in size.
77 USER-NAME PIC A(20).
* The standard input variables for comouting interest.
* The principal will be a 6-digit whole number, while
* the interest rate and years will be 2-digit whole numbers.
77 PRINCIPAL PIC 9(6).
77 INTEREST-RATE PIC 9(2).
77 YEARS PIC 9(2).
* And finally, variables to hold the results. Both will be
* 5-figure numbers with 2 decimal places.
77 SIMPLE-INTEREST PIC 9(5).99.
77 COMPOUND-INTEREST PIC 9(5).99.
PROCEDURE DIVISION.
* Actual code goes here!
MAIN-PROCEDURE.
PERFORM GET-NAME
PERFORM GET-LOAN-INFO
PERFORM CALCULATE-INTEREST
PERFORM SHOW-RESULTS
GOBACK.
* Get the user’s name, just to demonstrate getting a string
* value via keyboard input and storing it in a variable.
GET-NAME.
DISPLAY "Welcome to Bank of Murica!"
DISPLAY "What’s your name?"
ACCEPT USER-NAME
DISPLAY "Hello, " USER-NAME "!".
* Get the necessary info to perform an interest calculation.
GET-LOAN-INFO.
DISPLAY "What is the principal of your loan?"
ACCEPT PRINCIPAL
DISPLAY "What is the interest rate (in %)"
ACCEPT INTEREST-RATE
DISPLAY "How many years will you need to pay off the loan?"
ACCEPT YEARS.
* Do what who-knows-how-many lines of COBOL have been
* doing for decades, and for about 95% of all ATM transactions.
CALCULATE-INTEREST.
COMPUTE SIMPLE-INTEREST = PRINCIPAL +
((PRINCIPAL * YEARS * INTEREST-RATE) / 100) -
PRINCIPAL
COMPUTE COMPOUND-INTEREST = PRINCIPAL *
(1 + (INTEREST-RATE / 100)) ** YEARS -
PRINCIPAL.
SHOW-RESULTS.
DISPLAY "Here’s what you’ll have to pay back."
DISPLAY "With simple interest: " SIMPLE-INTEREST
DISPLAY "With compund interest: " COMPOUND-INTEREST.
* Yes, this needs to be here, and the name of the program
* must match the name specified in the PROGRAM-ID line
* at the start of the program, or COBOL will throw a hissy fit.
END PROGRAM STUPID-INTEREST-CALCULATOR.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment