ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

COBOL Recursive Factorial Calculation

COBOL

Goal -- WPM

Ready
Exercise Algorithm Area
1IDENTIFICATION DIVISION.
2PROGRAM-ID. RECURSIVE-FACTORIAL.
3AUTHOR. Admin.
4
5DATA DIVISION.
6WORKING-STORAGE SECTION.
701 INPUT-NUMBER PIC 9(3) VALUE 0.
801 RESULT-FACTORIAL PIC 9(10) VALUE 0.
901 N PIC 9(3) VALUE 0.
1001 MAX-FACTORIAL-INPUT PIC 9(3) VALUE 12. *> Factorial of 12 is the largest that fits in PIC 9(10)
11
12PROCEDURE DIVISION.
13MAIN-LOGIC.
14DISPLAY "Enter a non-negative integer (0-12) to calculate its factorial:".
15ACCEPT INPUT-NUMBER.
16
17IF INPUT-NUMBER < 0 OR INPUT-NUMBER > MAX-FACTORIAL-INPUT
18DISPLAY "Input out of range. Please enter a number between 0 and " MAX-FACTORIAL-INPUT.
19GO TO MAIN-LOGIC
20END-IF.
21
22MOVE INPUT-NUMBER TO N.
23CALL 'CALCULATE-FACTORIAL' USING N, RESULT-FACTORIAL.
24
25DISPLAY "The factorial of " INPUT-NUMBER " is: " RESULT-FACTORIAL.
26
27STOP RUN.
28
29*>--------------------------------------------------------------------*
30*> SUBROUTINE: CALCULATE-FACTORIAL
31*> Calculates the factorial of a non-negative integer recursively.
32*>--------------------------------------------------------------------*
33PROCEDURE CALCULATE-FACTORIAL.
34USING N RESULT-FACTORIAL.
35
36*> Base Case: Factorial of 0 or 1 is 1.
37IF N = 0 OR N = 1
38MOVE 1 TO RESULT-FACTORIAL
39GOBACK
40END-IF.
41
42*> Recursive Step: Factorial(N) = N * Factorial(N-1).
43SUBTRACT 1 FROM N.
44CALL 'CALCULATE-FACTORIAL' USING N, RESULT-FACTORIAL.
45
46*> Multiply N by the result of the recursive call.
47MULTIPLY N BY RESULT-FACTORIAL.
48
49*> Restore N for potential future calls or debugging (though not strictly needed here).
50ADD 1 TO N.
51
52GOBACK.
Algorithm description viewbox

COBOL Recursive Factorial Calculation

Algorithm description:

This COBOL program calculates the factorial of a given non-negative integer using recursion. The `CALCULATE-FACTORIAL` subroutine is designed to call itself with a decremented input until it reaches the base case (0 or 1). The results are then multiplied back up the call stack to produce the final factorial value. This demonstrates a classic recursive algorithm pattern in COBOL, suitable for understanding function calls and stack behavior.

Algorithm explanation:

The factorial of a non-negative integer N, denoted by N!, is the product of all positive integers less than or equal to N. The recursive definition is N! = N * (N-1)! for N > 0, and 0! = 1. The COBOL program implements this using a subroutine `CALCULATE-FACTORIAL`. The base case is when N is 0 or 1, returning 1. Otherwise, it recursively calls itself with N-1, then multiplies the result by N. The `MAIN-LOGIC` section handles input validation, ensuring the number is within a safe range (0-12) to prevent overflow with a `PIC 9(10)` result field. The time complexity is O(N) because each number from N down to 1 is processed once. The space complexity is also O(N) due to the recursion depth creating stack frames. Edge cases include 0! and 1!, which are correctly handled by the base case. The input validation prevents issues with negative numbers and potential overflows for larger inputs.

Pseudocode:

PROCEDURE CALCULATE-FACTORIAL(N):
  IF N is 0 OR N is 1:
    RETURN 1
  ELSE:
    RETURN N * CALCULATE-FACTORIAL(N - 1)
  END IF
END PROCEDURE