Professor Juarez wants to calculate the factorial numbers in his mathematic class. As an IT student, you are the best candidate to help him solve his problem. With the aid of your Visual basic expertise, write a program that will print the factorial of a number by defining a method named 'Factorial'. You should name your program Factorial. Factorial of any number n is represented by n! and is equal to 1*2*3*....*(n-1)*n. E.g.- 4! = 1*2*3*4 = 24 3! = 3*2*1 = 6 2!=2*1=2 Also, 1! = 1, 0! = 0
Module Module1
Sub Main()
'Get the number
Console.Write("Enter the number: ")
Dim number As Int64 = Int64.Parse(Console.ReadLine())
'Display the factorial
Console.WriteLine("{0}! = {1}", number, Factorial(number))
Console.ReadLine()
End Sub
''' <summary>
''' This functon calculates Factorial
''' </summary>
''' <param name="number"></param>
''' <returns></returns>
''' <remarks></remarks>
Private Function Factorial(ByVal number As Int64) As Int64
If number <= 0 Then
Return 0
ElseIf number <= 1 Then
Return 1
Else
Return number * Factorial(number - 1)
End If
End Function
End Module
Comments
Leave a comment