用VB编写 VB计算公式S=1+1/3+1/5+1/7+.+1/2n-1的和,当第n项的值
问题描述:
用VB编写 VB计算公式S=1+1/3+1/5+1/7+.+1/2n-1的和,当第n项的值
答
Private Sub Command1_Click()
Dim sum As Double
Dim i As Long
Dim n As Double
n = ((1 / (10 ^ -5)) + 1) / 2 '正好等于10 ^ -5时候n的值
n = Round(n + 0.5, 0) '对n+0.5进行四舍五入处理,保证n只要有小数点后面有数就进,不舍.
For i = 1 To n '计算n项的和
sum = sum + 1 / (2 * i - 1)
Next i
Print "项数n为:" & n
Print "n项和为:" & sum
End Sub
调试如下图: