Definitions

   The factorial of   0   (zero)   is defined as being   1   (unity).
   The   Factorial Function   of a positive integer,    n,    is defined as the product of the sequence:
                 n,   n-1,   n-2,   ...   1 



Task

Write a function to return the factorial of a number.

Solutions can be iterative or recursive.

Support for trapping negative    n    errors is optional.



Related task

   Primorial numbers


 
' Task: Factorial
' Language: Gambas
' Author: Sinuhe Masan (2019)
' Function factorial iterative
Function factorial_iter(num As Integer) As Long
  Dim fact As Long
  Dim i As Integer
 
  fact = 1
  If num > 1 Then
    For i = 2 To num
      fact = fact * i
 
    Next
 
  Endif
 
  Return fact
 
End
 
' Function factorial recursive
Function factorial_rec(num As Integer) As Long
 
  If num <= 1 Then
    Return 1
 
  Else
    Return num * factorial_rec(num - 1)
 
  Endif
 
End
 
 
Public Sub Main()
  Print factorial_iter(6)
  Print factorial_rec(7)
 
End
 