By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,406 Members | 2,660 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,406 IT Pros & Developers. It's quick & easy.

gcd sub not working

P: n/a
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.
Nov 21 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a

"Steve" <kg******@hotmail.com> wrote
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)

Nov 21 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.