Function Multiply & MultiplyR Learn programming Visual Basic (VB.net)

Lesson:

Functions


Exercise:

Function Multiply & MultiplyR 18


Objetive:

Create two functions, Multiply and MultiplyR, to calculate the product of two numbers using sums. T he first version must be iterative, and the second one must be recursive.


Code:

Imports System
Public Class exercise133
    Public Shared Function MultiplyR(ByVal n1 As Integer, ByVal n2 As Integer) As Integer
        If n2 = 0 Then
            Return 0
        Else
            Return n1 + MultiplyR(n1, n2 - 1)
        End If
    End Function

    Public Shared Function Main(ByVal args As String()) As Integer
        If args.Length <> 2 Then
            Console.WriteLine("Enter two numbers!!")
            Return 1
        Else
            Dim n1 As Integer = Convert.ToInt32(args(0))
            Dim n2 As Integer = Convert.ToInt32(args(1))
            Console.WriteLine(MultiplyR(n1, n2))
            Return 0
        End If
    End Function
End Class