Write a program that computes the factorial of a number n, or simply n!.
We can recursively define the factorial of a number as:
In symbolic terms:
By the above definition, the factorial of 4 is 24
Input will contain a single integer n where 0 ≤ n < 10. Output should contain a line with the factorial of n.
4
24
6
720
factorial
functionYour program should contain a factorial
function
that takes an integer and returns an integer.
Please refer to the information for your chosen language:
int factorial(int n);
factorial :: Int -> Int
def factorial(n):
int factorial(int n);
public static int Factorial(int x)
inside a public class Program
public static int factorial(int x)
inside a public class Factorial
function factorial(x)
function factorial (n)
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.
factorial
functiontry next: power1
Copyright © 2020-2022 Rudy Matela
This text is available under the CC BY-SA 4.0 license.
Originally available on cscx.org/factorial1