2009年4月8日 星期三

找出1-30間的質數

'找出1-30間的質數
Public Class Form1
    Dim i
    Dim n
    Dim str1, str2
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        str1 = "質數:"
        str2 = "非質數:"
        For n = 1 To 30
            Dim c = 0
            For i = 1 To n
                If n Mod i = 0 Then
                    c = c + 1
                End If
            Next
            If c = 2 Then
                str1 = str1 & n & "  "
            Else
                str2 = str2 & n & "  "
            End If
        Next
        MsgBox(str1 & vbNewLine & str2)
    End Sub
End Class

沒有留言:

張貼留言