COBOL/Loops

Loops in COBOL
In computer science, a loop is a sequence of statements which is specified once but which may be carried out several times in succession. The code "inside" the loop (the body of the loop) is executed a specified number of times, or once for each of a collection of items, or until some condition is met, or indefinitely.

COBOL provides a variety of loop control statements, including:
 * PERFORM THRU—execute a series of paragraphs by giving the first and last paragraph names in the sequence. After executing the last paragraph, the control is returned.
 * PERFORM UNTIL—a paragraph is executed until the given condition becomes true. The default condition is ‘With test before’ and it indicates that the condition is checked before executing statements in a paragraph.
 * PERFORM TIMES—a paragraph will be executed the number of times specified.
 * PERFORM VARYING—a paragraph will be executed until the condition in UNTIL phrase becomes true.

The example program demonstrates both the PERFORM UNTIL structure and the PERFORM VARYING structure. Note the counter is incremented in the UNTIL-LOOP paragraph, and not directly within the PERFORM UNTIL loop statement.

The loops lesson in the Wikiversity course Programming Fundamentals provides additional background, readings, multimedia resources, key terms, and activities related to understanding and using loops.

Please study those materials to better understand and extend the following example program.

Try It
Copy and paste the code above into one of the following free online development environments or use your own compiler / interpreter / IDE.
 * JDoodle
 * paiza.IO
 * TutorialsPoint

Assignment
Part 1:

In mathematics, the factorial of a positive integer $n$, denoted by $n!$, is the product of all positive integers less than or equal to $n$:
 * $$n! = n \times  (n-1)  \times (n-2)  \times  (n-3) \times \cdots \times  3 \times  2 \times  1 \,. $$

For example,
 * $$5! = 5 \times  4  \times  3  \times  2  \times  1 = 120 \,. $$

The value of 0! is 1, according to the convention for an empty product.

Modify the example program to compute the factorial of a number supplied as input.

Run and test the program. Be sure to use 0, negative numbers, non-integers, and large numbers as test cases.

Part 2:

In mathematics, the Fibonacci numbers, commonly denoted $F_{n}$, form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is,
 * $$F_0=0,\quad F_1= 1,$$

and
 * $$F_n=F_{n-1} + F_{n-2},$$

for $n > 1$.

The beginning of the sequence is thus:
 * $$0,\;1,\;1,\;2,\;3,\;5,\;8,\;13,\;21,\;34,\;55,\;89,\;144,\; \ldots$$

Modify the example program to compute and display the first n numbers in the Fibonacci sequence.

Run and test the program.

Part 3:

An amortization schedule is a table detailing each periodic payment on an amortizing loan (typically a mortgage), as generated by an amortization calculator. Amortization refers to the process of paying off a debt (often from a loan or mortgage) over time through regular payments. A portion of each payment is for interest while the remaining amount is applied towards the principal balance. The percentage of interest versus principal in each payment is determined in an amortization schedule. The schedule differentiates the portion of payment that belongs to interest expense from the portion used to close the gap of a discount or premium from the principal after each payment.

The formula for calculating the payment amount is shown below.
 * $$ A = P \frac{r(1+r)^n }{(1+r)^n -1} $$

where
 * A = payment Amount per period
 * P = initial Principal (loan amount)
 * r = interest rate per period
 * n = total number of payments or periods

Example: What would the monthly payment be on a 5-year, $20,000 car loan with a nominal 7.5% annual interest rate?

P = $20,000 r = 7.5% per year / 12 months = 0.625% per period n = 5 years * 12 months = 60 total periods The resulting amortization schedule is partially shown here:

Modify the example program to compute and display an amortization table. The program should request the payment amount per period, initial principal (loan amount), interest rate per, period, and the total number of payments or periods as inputs.

Run and test the program