试用VB程序语言写出主要程序段:求S=1+3+5+7+……+999的值。

2025-02-06 06:40:00
推荐回答(5个)
回答1:

你先建的command text控件
(1)Private Sub Command1_Click()
Dim x As Integer, i As Integer
x = Val(Text1.Text)
If x > 1 Then
For i = 2 To x - 1
If x Mod i = 0 Then Exit For
Next i
If i = x Then
MsgBox x & "是素数"
Else
MsgBox x & "不是素数"
End If
Else
MsgBox "x必须大于1"
End If
End Sub
要采纳哦 !

回答2:

(1)
dim a as integer
a=val(text1.text)
b=1
for i=2 to a/2
if a mod i = 0 then
b=b+1
exit for
end if
next
if b=1 then
msgbox a & "是素数"
else
msgbox a & "不是素数"
end if
(2)
dim s as integer
dim c as integer
s=0
for i=1 to 99
c=1
for j=1 to i
c=c*j
next
s=s+a
next

回答3:

1.
NumBool=true
for i = 2 to Num/2
if Num mod i = 0 then
msgbox Num & "不是一个素数"
NumBool =False
Exit For
end if
next i
if NumBool then msgbox Num & "是一个素数"
2.
Dim Num as long
Dim NumE as long
Num=0
for i = 1 to 99
NumE=1
for j = 1 to i
NumE=NumE*j
next j
Num=Num+NumE
next i

回答4:

1.
dim flag as bealoon
flag=false
for i=2 to (x-1)
if x mod i=0 then
flag=true
endif
next i

if flag=true then
msgbox X & " 不是素数!"
else
msgbox X & " 是素数!"

end if

2.
Dim x as long
Dim y as long
Y=0
for i = 1 to 99
x=1
for k = 1 to i
x=x*j
next j
y=y+x
next i

回答5:

Sub
a()
Dim
T
T
=
1
For
i
=
3
To
1001
If
i
<
1001
Then
T
=
T
+
i
Else
Cells(1,
1)
=
T
End
If
i
=
i
+
1
Next
i
End
Sub
这个是在EXCEL里运行的结果,算出来是250000,你可以把里面的Cells(1,1)替换成你想输出的地方MsgbBox
(“T”)