A Collection of Code Snippets in as Many Programming Languages as Possible

This project is maintained by TheRenegadeCoder

Welcome to the Factorial page! Here, you'll find a description of the project as well as a list of sample programs written in various languages.

This article was written by:

- Jeremy Grifski

The factorial of an integer `n`

is defined as:

`n! = 1 x 2 x 3 x 4 x ... x n`

With the special case `0! = 1`

.

You must write an executable program that accepts an integer `n`

on standard
input via the command line, and outputs `n!`

to standard output.

Note that the factorial function grows very quickly. For example, `4! = 24`

but `8! = 40320`

. Therefore, you should impose a limit on the input, so that
the largest factorial still fits into your language's largest supported datatype.

Also note that the factorial is not defined for negative integers.

Every project in the Sample Programs repo should be tested. In this section, we specify the set of tests specific to Factorial. In order to keep things simple, we split up the testing as follows:

- Factorial Valid Tests
- Factorial Invalid Tests

Description | Input | Output |
---|---|---|

Sample Input: Zero | "0" | "1" |

Sample Input: One | "1" | "1" |

Sample Input: Four | "4" | "24" |

Sample Input: Eight | "8" | "40320" |

Sample Input: Ten | "10" | "3628800" |

Description | Input |
---|---|

No Input | |

Empty Input | "" |

Invalid Input: Not A Number | "asdf" |

Invalid Input: Negative | "-1" |

All of these tests should output the following:

```
Usage: please input a non-negative integer
```

- Factorial in Algol68
- Factorial in Bash
- Factorial in Beef
- Factorial in C
- Factorial in C#
- Factorial in C++
- Factorial in Cobol
- Factorial in Coffeescript
- Factorial in Commodore Basic
- Factorial in D
- Factorial in Elixir
- Factorial in Erlang
- Factorial in Euphoria
- Factorial in Fortran
- Factorial in Go
- Factorial in Groovy
- Factorial in Haskell
- Factorial in Java
- Factorial in Javascript
- Factorial in Julia
- Factorial in Kotlin
- Factorial in Lisp
- Factorial in Lua
- Factorial in Mathematica
- Factorial in Objective C
- Factorial in Octave
- Factorial in Pascal
- Factorial in Perl
- Factorial in Php
- Factorial in Python
- Factorial in R
- Factorial in Rust
- Factorial in Scala
- Factorial in Typescript