Intermediate Exercise
Make an application to access the value of a factorial function $N!$
This is a classic exercise in job selection processes.
Remember that in most cases there is more than one way to solve the exercise.
Hint: Start the process from 0!
First, we create a function and define the input data as Integer and the output data as Long (as the number may increase considerably)
Function FacTorial(N As Long) As Double
We declare the variables and assign the value 1 to the variable Result. Remember that zero factorial equals to one (0! = 1)
Dim i As Double
Dim Result As Double
Result = 1
We create a For Next loop, whose final value (N!) comes from the input of the function, in which each iteration advance a step in the multiplication (accumulating the result in the variable Result).
For i = 1 To N
Result = Result * i
Next i
FacTorial = Result
Function FacTorial(N As Integer) As Double
Dim i As Integer
Dim Result As Double
Result = 1
For i = 1 To N
Result = Result * i
Next i
FacTorial = Result
End Function
SuperExcelVBA.com is learning website. Examples might be simplified to improve reading and basic understanding. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. All Rights Reserved.
Excel ® is a registered trademark of the Microsoft Corporation.
© 2019 SuperExcelVBA | ABOUT
Thank you for contributing. A message was sent reporting your comment.