gcd sub not working

S

Steve

I am using Label1 = 15 and Label2 = 18. When I run the procedure I get a
value of 0. I should get 3. I can't figure out what is going wrong.

Sub gcd()

Dim tmp As Long

Dim remainder As Long

Dim a As Integer

Dim b As Integer

Dim c As Integer

' Make a >= b.

a = Label1.Text

b = Label2.Text

If a < b Then

tmp = a

a = b

b = tmp

End If

' Pull out remainders.

Do Until remainder = 0

remainder = a Mod b

If remainder = 0 Then

c = b

Else

a = b

b = remainder

End If

Loop

End Sub

Thanks for any help.
 
L

Larry Serflaten

Steve said:
I am using Label1 = 15 and Label2 = 18. When I run the procedure I get a
value of 0. I should get 3. I can't figure out what is going wrong.

Compare your code with this:

HTH
LFS

Dim x, y, r As Integer

' get values
x = CInt(Label1.Text)
y = CInt(Label2.Text)

' order large to small
If x < y Then
r = y
y = x
x = r
End If

' find GCD
Do
r = x Mod y
x = y
y = r
Loop Until r = 0

' return GCD
Console.WriteLine(x)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top