vb简单编程求救!

来源:百度知道 编辑:UC知道 时间:2024/07/04 18:20:21
从键盘上输入两个正整数M和N,求最大公因子

用辗转相除法,程序如下:
Private Sub Command1_Click()
Dim M As Integer, N As Integer, r As Integer
M = InputBox("请输入M的值:")
N = InputBox("请输入N的值:")
Print Fcb(M, N)
End Sub

Private Function Fcb(ByVal M As Integer, N As Integer) As Integer
Dim t As Integer
If M < N Then
t = M: M = N: N = t
End If
If M Mod N = 0 Then
Fcb = N
Exit Function
End If
Do While M Mod N <> 0
t = M Mod N
M = N
N = t
Loop
Fcb = t
End Function

Private Sub Form_Click()
Dim M As Integer, N As Integer
M = InputBox("输入正整数M")
N = InputBox("输入正整数N")
Print M; "和"; N; "的最大公因子是";
Do
r = M Mod N
M = N
N = r
Loop Until r = 0
Print M

我也是新手