VB编程 要求输入两个数,并求出它们的最大公约数和最小公倍数。
Function GYS(lNum1 As Long, lNum2 As Long) As Long
创新互联公司专注于沿河企业网站建设,成都响应式网站建设公司,电子商务商城网站建设。沿河网站建设公司,为沿河等地区提供建站服务。全流程按需求定制开发,专业设计,全程项目跟踪,创新互联公司专业和态度为您提供的服务
Dim i As Long
For i = lNum1 To 1 Step -1
If lNum1 Mod i = 0 And lNum2 Mod i = 0 Then
GYS = i
Exit For
End If
Next
End Function
Function GBS(lNum1 As Long, lNum2 As Long) As Long
Do While i = False
s = s + 1
i = s Mod lNum1 = 0 And s Mod lNum2 = 0
Loop
GBS = s
End Function
Private Sub Command1_Click()
Dim m As Long, n As Long
m = Val(InputBox("输入m"))
n = Val(InputBox("输入n"))
Print "输入的两个数是 " m "和" n
Print "它们的最大公约数是";
If m n Then
Print GYS(m, n)
Else
Print GYS(n, m)
End If
Print "它们的最小公倍数是";
Print GBS(m, n)
End Sub
如何使用vb求两个数的最大公约数和最小
最大公约数
m = InputBox("输入第一个自然数")
n = InputBox("输入第二个自然数")
If m n Then t = m: m = n: n = t
r = m Mod n
Do While (r 0)
m = n
n = r
r = m Mod n
Loop
End if
MsgBox ("最大公约数为" n)
最小公倍数
Dim a%, b%, m%
a = InputBox("第一个数")
b = InputBox("第二个数")
For m = b To 1 Step -1
If a Mod m = 0 And b Mod m = 0 Then
Exit For
End If
Next
m = a * b / m
MsgBox "最小公倍数是" + CStr(m)
用VB求两个正整数的最大公约数和最小公倍数
Private Sub Form_Click()
Dim m As Long, n As Long
m = Val(InputBox("请输入第一个数:"))
n = Val(InputBox("请输入第二个数:"))
If m 1 Or n 1 Then MsgBox "两个数均需为正整数!": Exit Sub
MsgBox "最大公约数是" MaxGys(m, n) vbCrLf "最小公倍数是" MinGbs(m, n)
End Sub
Rem 求两个数的最大公约数
Private Function MaxGys(A As Long, B As Long)
Dim X As Long, Y As Long, K As Long
X = IIf(A = B, A, B) 'x存入最大值
Y = IIf(A = B, A, B) 'y 存入最小值
Do '辗转相除法
K = X Mod Y: If K = 0 Then Exit Do
X = Y: Y = K
Loop
MaxGys = Y
End Function
Rem 求两个数的最小公倍数
Private Function MinGbs(A As Long, B As Long)
MinGbs = A * B / MaxGys(A, B)
End Function
vb.net 求两个自然数的最大公约数和最小公倍数 辗转相乘法。
最大公约数
do until a=b
if ab then
a=a-b
elseif ba then
b=b-a
end if
loop
msgbox (a)
最小公倍数
c = a
22:
If a Mod b = 0 Or b Mod a = 0 Then
If a b Then
MsgBox(a)
Else
MsgBox(b)
End If
Else
a = a + c
GoTo 22
End If
辗转相乘法是什么玩意?
vb设计 输入两个正整数m和n,求他们的最大公约数和最小公倍数
Private Sub Command1_Click()
dim m,n,r ,min as integer
m=text1.text
n=text2.text
do
r=m mod n
m=n
n=r
loop until r=0
text3.text=m
min=val(text1.text)*val(text2.text)/m
text4.text=min
end if
(m是指第一个数,n是指第二个数,r是个参数,min是指最小公倍数,希望你能看懂)
最大公约数和最小公倍数vb代码如何写?
全部程序如下:
function gys(a as integer,b as integer) as integer
dim i as integer,j as integer,r as integer
i=a
j=b
do while i mod j=0
r=i mod j
i=j
j=r
loop
gys=j
end function
function gbs(dim a as integer,b as integer) as integer
dim r as integer
r=gys(a,b)
gbs=a*b/r
end function
Private Sub form1_Click()
dim a as integer,b as integer,c as integer,d as integer
a= Val(inputbox("输入第一个整数","输入",50))
b= Val(inputbox("输入第二个整数","输入",75))
c=gys(a,b)
d=gbs(a, b)
print a;"与";b;"的最大公约数是:";c
print a;"与";b;"的最大公倍数是:";d
End Sub
就是这些。
有什么问题请留言。
文章题目:vb.net最小公约数 求最大公约数vb代码
本文链接:http://scyingshan.cn/article/hgiggh.html