fork download
  1. def factorial(n):
  2. """
  3. Calculate the factorial of a non-negative integer n.
  4.  
  5. Parameters:
  6. n (int): A non-negative integer whose factorial is to be calculated
  7.  
  8. Returns:
  9. int: Factorial of the given number n
  10. """
  11. if n < 0:
  12. raise ValueError("Factorial is not defined for negative numbers")
  13. elif n == 0 or n == 1:
  14. return 1
  15. else:
  16. return n * factorial(n - 1)
  17.  
  18. # Example usage:
  19. # print(factorial(5)) # Output: 120
Success #stdin #stdout 0.03s 9560KB
stdin
Standard input is empty
stdout
Standard output is empty