用vb写一个求两个数的最大公约数和最小公倍数
时间: 2023-03-15 12:01:37 浏览: 133
答案:下面是用VB写的一个求两个数的最大公约数和最小公倍数的程序:Sub Main()
Dim a As Integer, b As Integer
Dim max As Integer, min As Integermax = 0
min = 0a = InputBox("请输入第一个数")
b = InputBox("请输入第二个数")For i = 1 To Min(a, b)
If a Mod i = 0 And b Mod i = 0 Then
max = i
End If
Nextmin = a * b / maxMsgBox "最大公约数:" & max & vbCrLf & "最小公倍数:" & minEnd Sub
相关问题
vb求最大公约数和最小公倍数
VB是一种编程语言,可以用来编写计算最大公约数和最小公倍数的程序。下面是一个简单的VB程序,可以计算两个数的最大公约数和最小公倍数:
```
Option Explicit
Dim m As Integer
Dim n As Integer
Dim t As Integer
Dim mn As Long
Dim r As Integer
Private Sub Command1_Click()
Label1.Caption = Val(mn / n)
End Sub
Private Sub Command2_Click()
Label2.Caption = Val(n)
End Sub
Private Sub Command3_Click()
m = Text1.Text
n = Text2.Text
mn = m * n
If m < n Then
t = m
m = n
n = t
End If
r = m Mod n
While r <> 0
m = n
n = r
r = m Mod n
Wend
Label1.Caption = Val(mn / n)
Label2.Caption = Val(n)
End Sub
Private Sub Command7_Click()
Label1.Caption = ""
Label2.Caption = ""
用vb写一个判断出各个数的最大约数不包过本身
你好,我是C知道,很高兴回答你的问题。
以下是使用VB编写一个判断出各个数的最大约数不包括本身的代码:
```
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim num As Integer = CInt(TextBox1.Text)
Dim i As Integer
Dim result As String = ""
For i = 1 To num - 1
If num Mod i = 0 And i <> num Then
result += i.ToString() + ","
End If
Next
If result.Length = 0 Then
result = "无最大约数"
Else
result = result.Substring(0, result.Length - 1)
End If
TextBox2.Text = result
End Sub
```
以上代码中,我们首先通过文本框获取一个整数,然后使用for循环依次判断从1到num-1之间的数是否为num的约数,如果是则将其加入到结果字符串中。最后将结果显示在另一个文本框中。
希望这个代码对你有所帮助。如果你有任何问题,请随时联系我。
相关推荐
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)