“求1到10的阶乘之和”用VB怎么编程序

1 x2 x4 x6 x8 x10
-- - --- + ---- - ---- + ------ - ----
1! 2! 4! 6! 8! 10!

谢谢大家.请大家帮帮忙了

第1个回答  推荐于2017-12-15
'先写一个求阶乘的函数
Private Function factorial(ByVal n As Integer) As Long
Dim i As Integer, l As Long
l = 1
For i = 1 To n
l = l * i
Next
factorial = l
End Function

'添加一个按钮控件
Private Sub Command1_Click()
Dim result As Long, i As Integer
result = 0
For i = 1 To 10
result = result + factorial(i)
Next
Form1.Print result
End Sub
'结果为4037913本回答被提问者采纳
第2个回答  2009-12-17
结果 22.2186507936508

Option Explicit

Private Function factorial(ByVal n As Long) As Double
Dim i As Long
Dim L As Double
For i = 1 To n
L = L + 1 / i
Next
factorial = L
End Function

Private Sub Command1_Click()
Dim result As Double, i As Long
result = 0
For i = 1 To 10
result = result + factorial(i)
Next
Debug.Print result
End Sub
相似回答