输出100到200之间的所有素数,并统计素数的个数及其所有素数的和。 要用VB程序编写,用上flag

2024-11-11 00:42:35
推荐回答(2个)
回答1:

Private Sub Command1_Click()
Dim a As Integer, b As Integer
flag = ""
For a = 100 To 200
b = 2
Do While a Mod b <> 0
b = b + 1
Loop
If b = a Then flag = flag + CStr(a) + " "
If Len(flag) = 40 Then
Form1.Print flag
flag = ""
End If
Next
End Sub

回答2:

Function flag(x As Integer) As Boolean
Dim i As Integer
For i = 2 To x - 1
If x Mod i = 0 Then
Exit For
End If
Next i
If i >= x Then
flag = True
Else
flag = False
End IfEnd Function
Private Sub Command1_Click()
Dim x As Integer
For x = 100 To 200
If flag(x) Then
Print x;
End If
Next x
End Sub