Advertisement
caasinehc

CSC301 Factorial Example Code Isaac Chen

Oct 1st, 2021
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.31 KB | None | 0 0
  1. PYTHON:
  2.  
  3. def factorial(n: int):
  4. prod: int = 1
  5. for i in range(1, n + 1):
  6. prod *= i
  7.  
  8. return prod
  9.  
  10. C++
  11.  
  12. int factorial(int n) {
  13. int prod = 1;
  14. for(int i = 1; i <= n; i++) {
  15. prod *= i;
  16. }
  17.  
  18. return prod;
  19. }
  20.  
  21. SML
  22.  
  23. fun factorial(n: int): int =
  24. if n = 0 then 1
  25. else n * factorial(n - 1);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement