VB编程:利用判素数函数求100-200之间所有素数的平均值
发布网友
发布时间:2024-10-16 06:11
我来回答
共1个回答
热心网友
时间:2024-10-21 00:03
Private Sub Command1_Click()
Dim i As Integer, avg As Single, n As Integer
For i = 100 To 200
If pd(i) Then
n = n + 1
avg = avg + i
End If
Next
avg = avg / n
MsgBox avg
End Sub
Private Function pd(ByVal shu As Integer) As Boolean
Dim i As Integer
For i = 2 To Sqr(shu)
If shu Mod i = 0 Then Exit For
Next
If i < Sqr(shu) Then
pd = False
Else
pd = True
End If
End Function