程序设计题

程序设计题1.求1-100之间所有的质数。

Private Sub Command1_Click()

Dim n%, i%, flag%, k%

For n = 1 To 100 Step 2

k = Int(Sqr(n))

i = 2

flag = 0

Do While i <= k

If n Mod i = 0 Then

flag = 1

End If

i = i + 1

Loop

If flag = 0 Then

Print n

End If

Next n

End Sub

2.计算1-100之间所有整数的和。

Private Sub Command1_Click()

Dim a As Integer

n = 0

For a = 1 To 100

n = n + a

Next a

Print n

End Sub

3.计算1-100之间所有奇数的和。

Private Sub Command1_Click()

Dim i, s1%

s1 = 0

For i = 1 To 100 Step 2

s1 = s1 + i

Next i

Print s1

End Sub

4.计算1-100之间所有偶数的和。

Private Sub Command1_Click()

Dim i, s1%

s1 = 0

相关推荐
相关主题
热门推荐