Skip to content
C

Factorial (Recursive and Iterative)

Easypython

Write two functions, factorialrecursive(n) and factorialiterative(n), both returning n!, and print both results for the same input to confirm they agree.

What the problem means: n! (n factorial) is the product of every whole number from 1 to n. There are two classic ways to compute it — recursion (a function calling itself) and iteration (a loop) — and this problem asks you to write both.

Approach: factorialrecursive multiplies n by factorialrecursive(n - 1), stopping at the base case n == 0 or n == 1. factorial_iterative just multiplies a running total in a loop from 2 to n.

Input: One line: a whole number n.

Output: Two lines: Recursive: <n!> Iterative: <n!>

Input (stdin)

Output

Run your code to see output here...