用vb输出20~100内的素数,要求5个一行

来源:百度知道 编辑:UC知道 时间:2024/05/21 06:54:09
渴望赐教!

dim a ,b ,d as integer
dim c as boolean
d=1
for a = 21 to 99 step 2
c=true
for b = 2 to int(sqra))
if a mod b = 0 then
c = false
end if
next b
if c=true then
if d <5 then
print a; '注意,这里有";"不换行
d=d+1
else
print a '这里换行
d=1
end if
end if
next a

Function prame(n As Integer) As Boolean '建立素数函数
Dim i As Integer
For i = 2 To n
If n Mod i = 0 Then
Exit For
End If
Next i
If i = n Then
prame = True
Else
prame = False
End If

End Function

Private Sub Form_Load()

Dim i As Integer
Dim j As Integer
Dim a(30) As Integer '定义数组存放素数
j = 0
For i = 20 To 100
If prame(i) = True Then
a(j) = i
j = j + 1
End If