index
| submit
| rank
| book

## factorial1 – Factorial of a number (easy version)

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 a single integer *n* where 0 ≤ n < 10.
Output should contain a line with the factorial of *n*.

#### Example input 1

```
4
```

#### Example output 1

```
24
```

#### Example input 2

```
6
```

#### Example output 2

```
720
```

### 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

- 2/12: works for the above examples albeit in an incorrect format
- 4/12: works for the above examples in the correct format
- 8/12: works for other examples
- 12/12: implements the
`factorial`

function

try first: good1 signum1

try next: power1

index
| submit
| rank
| book

Copyright © 2020-2023 Rudy Matela

All rights reserved