COMP2247 - Algorithms and Data Structures

Syllabus Software Installation Assignments Tests
Lectures and Labs
**Back to Full Menu**

Recursion Factorial Program Fibonacci Program Integer Power Program Tower of Hanoi Program Max Element Program Palindrome Program Reverse String Program Array Sum Program



FactorialWithRecursion Program


Description

This program inputs a positive integer n and then calculates n! using recursion technique.

n! = n * (n - 1)* (n - 2) * ... * 1

Associated Concepts:

Recursion

Algorithm:

main method:

Declare variables.

Input an integer n from the keyboard.

Call the recursive method factorial() with n as the argument.

Print the result.

factorial method definition:

If the method is call with the base case: n is 0 or n is 1, it returns the result.

Otherwise, the method returns n times factorial (n-1).

Source Code



Sample Run



Video