作业帮 > 综合 > 作业

设计求数列s=1+1/3+(-1/5)+1/7+1/9+(-1/11)+...前n(n>=1)项和的算法VB编程,用fo

来源:学生作业帮 编辑:大师作文网作业帮 分类:综合作业 时间:2024/11/10 01:06:44
设计求数列s=1+1/3+(-1/5)+1/7+1/9+(-1/11)+...前n(n>=1)项和的算法VB编程,用for next i 循环体编
用循环+选择结构,不要用floop float ftemp 之类的
设计求数列s=1+1/3+(-1/5)+1/7+1/9+(-1/11)+...前n(n>=1)项和的算法VB编程,用fo
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 Su