VB程序输入整数N,计算前N项和S=1+(1+2)+(1+2+3)+(1+2+3+4)+….
问题描述:
VB程序输入整数N,计算前N项和S=1+(1+2)+(1+2+3)+(1+2+3+4)+….
答
Private Sub Command1_Click()
Dim i,n,s As Integer
n = Val(Text1.Text)
s = 0
For i = n To 0 Step -1
s = s + i * (i + 1) / 2
Next i
Text2.Text = s
End Sub