index
| submit
| rank
| book

## factorial – Factorial of a number

Write a program that computes the factorial of a number *n*, or simply *n!*.

We can recursively define the factorial of a number as:

- The factorial of zero is one.
- The factorial of a natural number
*n* is the factorial of *n - 1*.

In symbolic terms:

By the above definition,
the factorial of 4 is 24

- 4! = 4 × 3!
- 4! = 4 × 3 × 2!
- 4! = 4 × 3 × 2 × 1!
- 4! = 4 × 3 × 2 × 1 × 0!
- 4! = 4 × 3 × 2 × 1 × 1
- 4! = 24.

### Input and Output

Input will contain several lines with a single integer *n* where 0 ≤ n ≤ 12.
Output should contain a line with the factorial of *n*.

### The `factorial`

function

Your program should contain a `factorial`

function
that takes an integer and returns an integer.
Please refer to the information for your chosen language:

- C prototype:
`int factorial(int n);`

- Haskell type:
`factorial :: Int -> Int`

- Python definition:
`def factorial(n):`

- C++ prototype:
`int factorial(int n);`

- C# definition:
`public static int Factorial(int x)`

inside a public class `Program`

- Java definition:
`public static int factorial(int x)`

inside a public class `Factorial`

- JavaScript definition:
`function factorial(x)`

- Lua definition:
`function factorial (n)`

- Ruby definition:
`def factorial(n)`

Specifically for this exercise when using Python, JavaScript, Lua or Ruby,
avoid using `sys.exit()`

, `process.exit()`

, `os.exit()`

or `exit`

,
as your program is appended with some extra assertions in one of the test sets.

### Scoring

- 1/6: works for the above example albeit in an incorrect format
- 2/6: works for the above example in the correct format
- 4/6: works for other examples
- 5/6: works for edge cases
- 6/6: implements the
`factorial`

function

try first: good oddeven factorial1

try next: power

index
| submit
| rank
| book

Copyright © 2020-2021 Rudy Matela

All rights reserved