Best Answer

' Iterative solution

Function iterativeFactorial(ByVal n As Long) As Long

Dim factorial As Long = 1

For i As Long = 1 To n

factorial *= i


Return factorial

End Function

' Recursive solution

Function recursiveFactorial(ByVal n As Long) As Long

If n <= 1 Then

Return n

End If

Return n * recursiveFactorial(n - 1)

End Function

User Avatar

Wiki User

15y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

16y ago

Factorial is a Mathematical Function.

Factorial returns the product of all numbers from 1 to itself

e.g. Factorial 5 = 5*4*3*2*1 = 120

It is expressed as n! = factorial of n

To implement it in Visual Basic, there are two methods-

Function factorial(ByVal n as Integer) as Integer

If n =< 1 Then factorial = 1:Exit Function

factorial = n * factorial(n-1)

End Function

Function factorial(ByVal n as Integer) as Integer

factorial = 1

Dim a as Integer

For a = 1 to n

factorial = factorial * a

Next 'a

End Function

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

dim num as integer, factorial as single

num=inputbox("enter a number")

factorial = 1

for x = 1 to num

factorial = factorial * x

next x

print"factorial is" ; factorial

or By Recursive Method

Private Function FindFactorial(number As Integer)

If number < 1 Then

FindFactorial = 1


FindFactorial = number * FindFactorial(number - 1)

End If

End Function

' recursive is faster and simpler for finding factorial

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is a factorial function in Visual Basic?
Write your answer...
Still have questions?
magnify glass
Related questions

What is a function procedure in Visual Basic?

A function is essentially a subroutine that is ment to be used by other subroutines.

What function in Visual Basic will return a date from a computer?

(it used to be) DATE$

What is rounding in Visual Basic?

Rounding in Visual Basic is the method of rounding an integer up, or flooring an integer, which is rounding down. To round up, you use the System.Math.Round function. To round down, or floor, you use the System.Math.Floor function.

What is visual basic control?

Visual Basic Controls work on Visual Studio for Visual Basic and Applications that made by Visual Basic.

How do you get square root in Microsoft visual basic?

The function is Sqr() in VB6 and Math.Sqr() in .NET.

Write a C-like program fragment that calculate the factorial function for argment 12 with do while loop?

#!/usr/bin/perl print factorial($ARGV[11]); sub factorial { my($num) = @_; if($num == 1) { return 1; # stop at 1, factorial doesn't multiply times zero } else { return $num * factorial($num - 1); # call factorial function recursively } }

The year of invention of visual basic?

Visual Basic was started in 1991.

Who developed visual basic?

Microsoft is the developer of visual basic

Who invented the visual basic?

Visual Basic was created by a team at Microsoft.

What is the difference between visual basic and visual c?

The programming language: Visual Basic is a BASIC-like (or BASIC-derived) language, Visual C is... well C.

Is Visual Basic 6.0 compatible with Visual Basic 2005?


Who is the developer of Visual Basic?

Microsoft is the developer of Visual Basic.