VB编程1-100求素数之和

RT,急用在线等!!!
是道笔试题,不是机试

第1个回答  2010-07-06
Private Sub Form_Click()
Dim i, j, b As Integer

For i = 1 To 100
For j = 2 To i - 1

If i Mod j = 0 Then Exit For

Next

If j > i - 1 Then b = b + i

Next
Print b

End SuB
最简单的本回答被提问者采纳
第2个回答  2010-07-06
Private Sub Command1_Click()
Dim flag As Boolean, S As Integer
For i = 2 To 100
flag = True
For j = 2 To Int(Sqr(i))
If i Mod j = 0 Then flag = False
Next
If flag Then S = S + i
Next
Print S
End Sub
第3个回答  2010-07-06
Function Prime(a As Integer) As Boolean
Dim k As Integer, isprime As Boolean
If a < 2 Then
isprime = False
Else
isprime = True
k = 2
Do While k < a
If a Mod k = 0 Then
isprime = False
Exit Do
Else
k = k + 1
End If
Loop
End If
Prime = isprime
End Function

Private Sub Command1_Click()
Dim i As Integer, sum As Integer
For i = 1 To 100
If Prime(i) Then
sum = sum + i
End If
Next i
Print sum
End Sub

求用VB , for next 语句写一个1到100之间的质数的和
Private Sub Form_click()Cls Dim sum As Integer Dim n As Integer For I = 1 To 100 If Pd(I) = True Then sum = sum + I n = n + 1 Print I;x = x + 1 If x Mod 10 = 0 Then Print End If Next Print Print "素数的和为:" & sum Print "共有" & n & "个"E...

VB编程求1--100之间的素数之和。。
Static a Dim c As Integer, d As Integer, bz As Integer For c = 1 To 100 bz = False If c \/ 2 = c \\ 2 Then bz = True End If For d = 3 To Int(Sqr(c)) Step 2 If c \/ d = c \\ d Then bz = True d = c End If DoEvents Next d If bz = False Then a...

vb编程输出1-100的所有素数,并计算这样的素数合计多少个,下面我写的...
'Mod是取余数,如果说由i来取余数不可能出现1 '你可以这样: If i Mod 2 = 0 Then '它出现0的可能有50次 Private Sub Command3_Click()For i = 1 To 100 If i Mod 2 = 0 Then Sum = Sum + 1 End If Next i Me.Caption = Sum End Sub ...

VB编程1-100求素数之和
Next If j > i - 1 Then b = b + i Next Print b End SuB 最简单的

vb题:编程计算100以内素数的和
1 To n '嵌套for循环 i为除数If n Mod i = 0 Then '如果 i 能整除nk = k + 1 '(k为可以整除n的个数) 则k加1End IfNext i '结束for循环If k = 2 Then '如果 k等于2 (由于素数只有两个能够整除的除数,为素数的特性)s = s + n 's相当于累加器,将满...

...100到200之间的整数,输出每行5个,判断是否为素数,求出素数之和...
To 100)j = 0sum = 0Print "生成的整数:"For i = 1 To 100 a(i) = Int(101 * Rnd + 100) Print a(i), j = j + 1 If (j = 5) Then Print j = 0 End IfNextj = 0Print "生成的数中的素数:"For i = 1 To 100 If (sushu(a(i))) The...

VB程序 求100内所有素数的和。
For m = 2 To 100 For i = 2 To m - 1 '判断是否是素数 If m Mod i = 0 Then Exit For End If Next i If i = m Then '如果是,相加 sum = sum + m End If Next m Print sum 另外楼主的代码有误,按你那个思路,代码如下 Dim sum As Integer For m = 2 To 100 i = ...

急!!VB中关于求素数和的程序
If I Mod J <> 0 Then X = X + 1 End If If X = I - 2 Then '如果全部不整除,说明是素数 S = S + I '计算素数和 End If Next J Next I Text1.Text = S '显示素数和 End Sub Private Sub Option1_Click()K1 = 100 K2 = 200 End Sub Private Sub Option2_Cl...

vb利用数组程序求100以内相邻两数之和是素数的所有自然数对
private function prime(byval n as integer)as bool prime=true for i=1 to sqr(n)if n mod i=0 then prime=false exit for endif end function dim s(1 to 100) as integer for i=1 to 100 s(i)=i next for i=1 to 99 if prime(s(i)+s(i+1)) then print s(i),s(i+...

vb编程题求100至200内的素数之和
做这个程序时,首先初始化一个美加器,初值为零,然后通过一个判断是否素数的函数,在做一个循环,从101到199步长为二进行循环,如果找到了一个素数,就把他累加起来,循环结束的时候,输出这个累加器的值就可以了

相似回答