Thursday, June 14, 2012

Find the Factorial without and with recursive


Function Fact()
n = int(InputBox(" Enter a number="))
' Coding for normal execution
f = 1
for i = 1 to n
f = f * i
next
msgbox f
'--------------------

' Coding for recursive logic
REM if n < 0 then
REM msgbox "Invalid input"
REM else
REM f = FactNumber(n)
REM msgbox "Factorial of " & n & " is " & f
REM end if

End Function

'------------------------------
' Recursive Function
Function FactNumber(n)

if n = 0 then
FactNumber = 1
else
FactNumber = n * FactNumber(n-1)
end if
End Function

No comments: