Pages

Search This Blog

Factorial using class witOUT constructor


Public Class Class2
    Function factorial(ByVal m As Integer) As Integer
        If m = 0 Then
            Return 1
        Else
            Return m * factorial(m - 1)
        End If
    End Function
End Class


Module Fact2UsClassNoConst
    Sub main()
        Dim n, fact As Integer
        Dim obj As New Class2
        Console.WriteLine("Using NO constructor")
        Console.WriteLine()
        Console.Write("Enter a Number : ")
        n = CInt(Console.ReadLine())
        If n < 0 Then
            Console.WriteLine("Factorial of negative number is NOT possible.")
        ElseIf n = 0 Or n = 1 Then
            Console.WriteLine("Factorial of " + n.ToString + " = 1")
        Else : fact = obj.factorial(n)
            Console.WriteLine("Factorial of " + n.ToString + " = " + fact.ToString())
        End If
        Console.ReadLine()
    End Sub
End Module

No comments:

Post a Comment