VB程序 求100内所有素数的和。

将下面求100内所有素数的VB语言改一下,改成求100内所有素数的和。
For m=2 to100
Do While m mod i<>0
i=i+1
Loop
If i=m then print m
next i
力求简单。谢谢。。

Dim m As Integer
Dim i As Integer
Dim sum As Integer
sum = 0
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 = 2
Do While m Mod i <> 0
i = i + 1
Loop
If i = m Then sum = sum + m
Next m
Print s
温馨提示:内容为网友见解,仅供参考
第1个回答  2011-03-06
Dim s As Integer
For m=2 to100
Do While m mod i<>0
i=i+1
Loop
If i=m then s=s+m
next i
print s
大概不需要解释吧。
第2个回答  2017-11-09
if i =m 那里的i 是2 to m-1。因此语句是错误的
第3个回答  2011-03-06
Private Sub Command1_Click()
For m = 2 To 100
i = 2
Do While m Mod i <> 0
i = i + 1
Loop
If i = m Then s = s + m
Next m
Print s
End Sub

VB程序 求100内所有素数的和。
Dim m As Integer Dim i As Integer Dim sum As Integer sum = 0 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 另外楼主的代码有误,按...

急需vb编程求100以内所有奇数和及所有偶数和
方法一:通过按钮“求2到100之间的偶数和”,代码如下:vb Private Sub Command1_Click()Dim i As Integer Dim s As Integer s = 0 For i = 2 To 100 Step 2 s = s + i Next i Print "2到100的所有偶数和是:" & s End Sub 方法二:通过按钮“方法2”,代码如下:vb Private Sub...

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

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 , for next 语句写一个1到100之间的质数的和
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 & "个"End Sub Private Function Pd(ByVal n As Integer) As Boolean Dim I ...

用vb 做 100以内质数求和 的程序
在窗体上添加个按钮 Private Sub Command1_Click()Dim i As Integer, s As Integer, j As Integer, l As Integer For i = 2 To 100 l = 0 For j = 2 To i - 1 If i Mod j = 0 Then l = 1 Next j If l <> 1 Then s = s + i Next i Print "100以内质数和为" & ...

VB编码找出100到200之间的素数,并求和,在线等
End If Next j If isSushu = True Then str = str & i & " " s = s + i End If Next i '窗体输出查找结果 Print "100到200之间的素数:" Print str & vbCrLf Print "素数之和=" & sEnd Sub运行效果 ...

跪求用VB编程计算1~100的奇数.偶数.整数和??
As Integer, sumO As Integer, sumZ As Integer, i As Integer Private Sub Form_Load()For i = 1 To 100 sumZ = sumZ + i If i Mod 2 = 0 Then sumO = sumO + i Else sumJ = sumJ + i End If Next Text1.Text = sumZ & vbCrLf & sumJ & vbCrLf & sumO End Sub ...

求vb程序 找出100以内的所有因子之和为完全平方数的正整数
Do While old > 1 For i = 2 To old If old Mod i = 0 Then chuli = chuli + i old = old \/ i If old < i Then Exit For i = i - 1 End If Next Loop End Function Private Sub Command1_Click()For old = 1 To 100 If Sqr(chuli(old)) = Int(Sqr(chuli(old))) ...

怎样用循环结构vb计算1加到100结果显示在文本框?
在VB编程语言中,要计算从1加到100的和并将其结果显示在文本框中,可以使用循环结构。下面的代码实现该功能。首先定义两个整型变量,`i`和`j`。`i`用于表示当前要累加的数字,而`j`用于累计所有数字的和。然后初始化`j`为0。这表示从一开始就将累计的和设为零。接下来使用`for`循环从1迭代到...

相似回答