fork download
  1. def factorial(n):
  2. """
  3. This function calculates the factorial of a non-negative integer.
  4.  
  5. Args:
  6. n: The non-negative integer for which to calculate the factorial.
  7.  
  8. Returns:
  9. The factorial of n, or 1 if n is 0.
  10. Raises ValueError if n is negative.
  11. """
  12. if n < 0:
  13. raise ValueError("Factorial is not defined for negative numbers")
  14. elif n == 0:
  15. return 1
  16. else:
  17. result = 1
  18. for i in range(1, n + 1):
  19. result *= i
  20. return result
  21.  
  22. # Example usage:
  23. number = 5
  24. fact = factorial(number)
  25. print(f"The factorial of {number} is {fact}")
Success #stdin #stdout 0.03s 9612KB
stdin
Standard input is empty
stdout
The factorial of 5 is 120