Develop an application to compile the Sum of the first n terms of the series :
S=1-3+5-7+9- .......
Also refer Sum of n numbers .
S=1-3+5-7+9- .......
(A)Program Design :
(B) Property(Controls Used) :
- 3 labels with Captions as S=1-3+5-7+9- ....... , Terms & Generate
- 2 Text Boxes
- One Command Button with Caption Generate
(C) Attaching Code to the Object :
Private Sub Command1_Click() Dim sum, j, i As Integer On Error GoTo error Text2 = "" sum = 0 j = 1 For i = 1 To Int(Text1.Text) sum = sum + j If (j > 0) Then If (i <> 1) Then Text2 = Text2 + "+" + Str(j) Else Text2 = Text2 + " " + Str(j) End If j = -(j + 2) Else Text2 = Text2 + " " + Str(j) j = -j + 2 End If Next Text2 = "Series is..." + vbCrLf + Str(sum) + "=" + Text2 error: End Sub |
(D) Output :
Also refer Sum of n numbers .
=================================================================
No comments:
Post a Comment