第3題解題程式
FOR迴圈版
Private Sub cmd_no3_Click()
'**************************
'*119-940303 Program Start*
'**************************
Open "A:\940303.SM" For Input As #1
Input #1, n
Close #1
For a = 2 To n - 1 '用2到N-1去除N
If (n Mod a) = 0 Then '如果N可以被2到N-1整除,N就不是質數
S = n & " is not a prime number" '顯示N不是質數
Exit For
End If
S = n & " is a prime number" '如果2到N-1都不能整除N,N就是質數
Next
Txt_no3.Text = "第三題結果:" & S
End Sub
DO WHILE 迴圈版
Private Sub cmd_no3_Click()
'**************************
'*119-940303 Program Start*
'**************************
Open "A:\940303.SM" For Input As #1
Input #1, n
Close #1
a = 2
Do While a < n
If (n Mod a) = 0 Then
S = n & " is not a prime number"
Exit Do
End If
a = a + 1
S = n & " is a prime number"
Loop
Txt_no3.Text = "第三題結果:" & S
End Sub
DO WHILE後測試版
Private Sub Cmd_no3_Click()
'**************************
'*119-940303 Program Start*
'**************************
Open "A:\940303.SM" For Input As #1
Input #1, n
Close #1
a = 2
Do
If (n Mod a) = 0 Then
S = n & " is not a prime number"
Exit Do
End If
a = a + 1
S = n & " is a prime number"
Loop While a < n
txt_no3.Text = "第三題結果:" & S
End Sub
執行畫面