设计求数列s=1+1⼀3+(-1⼀5)+1⼀7+1⼀9+(-1⼀11)+...前n(n>=1)项和的算法VB编程,用for next i 循环体编

谢谢用循环+选择结构,不要用floop float ftemp 之类的谢谢
2024-12-13 02:23:39
推荐回答(2个)
回答1:

Private Sub Command1_Click()
Dim n As Long
Dim i As Long
Dim sum As Double

n = Val(Text1.Text)
sum = 1#
For i = 2 To n
'sum = sum + (-1) ^ i * 1 / (2 * (i - 1) + 1)
If 2 * Int(i / 2) = i Then
sum = sum + 1 / (2 * (i - 1) + 1)
Else
sum = sum - 1 / (2 * (i - 1) + 1)
End If
Next i
Label1.Caption = sum
End Sub

回答2:

s=0
for i=1 to n step 6
s=s+1/i+1/(i+2)-1/(i+4)
next i