I write 4 code as follow to solve LCM/GCL but I don't know reason why the answer is wrong with various location on Dim r as Integer?
Code:
[FONT=Courier New]Dim m As Integer
Dim n As Integer
Dim r As Integer 'location 1
Sub GCD(m, n)
r = m Mod n
If r = 0 Then
MsgBox n & " is GCD" 'Wrong ans.
Else
Call GCD(n, r)
End If
End Sub
Private Sub CommandButton1_Click()
m = InputBox("Input an interger m ")
n = InputBox("Input an interger n")
Call GCD(m, n)
End Sub[/FONT]
Code:
[FONT=Courier New][FONT=Courier New]Dim m As Integer
Dim n As Integer
Sub GCD(m, n)
Dim r As Integer [FONT=Courier New]'location 2[/FONT]
r = m Mod n
If r = 0 Then
MsgBox n & " is GCD" 'Correct ans.
Else
Call GCD(n, r)
End If
End Sub[/FONT]
Private Sub CommandButton1_Click()
m = InputBox("Input an interger m ")
n = InputBox("Input an interger n")
Call GCD(m, n)
End Sub[/FONT]
Code:
[FONT=Courier New]Dim m As Integer
Dim n As Integer
Sub GCD(m, n)
r = m Mod n
If r = 0 Then
MsgBox n & " is GCD" 'Correct ans.
Else
Call GCD(n, r)
End If
End Sub
Private Sub CommandButton1_Click()
Dim r As Integer [FONT=Courier New]'location 3[/FONT]
m = InputBox("Input an interger m ")
n = InputBox("Input an interger n")
Call GCD(m, n)
End Sub[/FONT]
Code:
[FONT=Courier New]Dim m As Integer
Dim n As Integer
Dim r As Integer 'location 4
Sub GCD(m, n)
Do
r = m Mod n
If r <> 0 Then
m = n: n = r
End If
Loop While r <> 0
MsgBox n & " is GCD" 'Correct ans.
End Sub
Private Sub CommandButton1_Click()
m = InputBox("Input an interger m ")
n = InputBox("Input an interger n")
Call GCD(m, n)
End Sub[/FONT]