692. Product of First N Natural Numbers

EasyRecursionRecursion

Given a positive integer N, return the product of the first N natural numbers (1 × 2 × 3 × ... × N) using recursion. Note: this is equivalent to N! (N factorial). The result can be extremely large — return the exact integer.

Input: A single positive integer N (1 ≤ N ≤ 70).

Output: A single integer — the product 1 × 2 × ... × N.

Examples

Example 1
Input: 5
Output: 120
Explanation: 1×2×3×4×5=120. Recursion: product(5)=5×product(4)=5×24=120.
Example 2
Input: 1
Output: 1
Explanation: Base case: product(1)=1.
Example 3
Input: 10
Output: 3628800
Explanation: 10! = 3628800.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →