求两个数的最大公约数和最小公陪数VB程序

来源:百度知道 编辑:UC知道 时间:2024/05/14 10:04:13

Private Sub Command1_Click()
Dim S As Long

S = YueOrBei(24, 32)
MsgBox "24 和 32 的最大公约数是:" & S, vbInformation

S = YueOrBei(24, 32, True)
MsgBox "24 和 32 的最小公倍数是:" & S, vbInformation
End Sub
Private Function YueOrBei(J1 As Long, J2 As Long, Optional IsBei As Boolean) As Long
'IsBei=True 返回公倍数(最小),否则返回公约数(最大)
Dim S As Long, S1 As Long, S2 As Long

S1 = J1: S2 = J2
Do
S = S1 Mod S2
If S = 0 Then YueOrBei = S2: Exit Do
S1 = S2: S2 = S
Loop

If IsBei Then YueOrBei = J1 * J2 / YueOrBei
End Function