| n! | = | 1 if n=1 |
| = | n × (n-1)! otherwise |
| Racket | C++ |
|---|---|
(define factorial (lambda (n) (if (= n 1) 1 (* n (factorial (- n 1)))))) |
int factorial(int n)
{
if (n == 1)
return 1;
else
return n * factorial(n-1);
}
|
Note that the equality test in C++ is "==".
This is because the single equals sign "=" signifies an assignment statement.