Series Calculations with Loops Code ' ' This program accepts an integer as input and then computes the value for a number of series ' ' The input is tested to make sure it is a positive value ' ' 1. The sum = 1 + 2 + 3 + 4 + . . . N is computed by each of the three loop structures: ' Do - While ' Do - Until ' For - Next ' ' 2. Loop ' 3. Loop ' 4. Next ' 5. Do ' 6. Do ' The sum of the Odd integers 1 + 3 + 5 + 7 + . . . N is computed with a For - Next The sum of the Even integers 2 + 4 + 6 + 8 + . . . N is computed with a For - Next The sum of the fractions 1 + 1/2 + 1/3 + 1/4 + . . . 1/N is computed with a For Loop All perfect squares less than N ( 1, 4, 9, 16 etc. ) are computed and printed with a While Loop The sum of the perfect square fractions ( 1 + 1/4 + 1/9 + . . . is computed with a While Loop Public Class frmSum Private Sub btnQuit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnQuit.Click Close() ' NOTE: This will terminate the program just like "End" End Sub Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click lstOut.Items.Clear() End Sub Private Sub bthDoWhile_Click(sender As System.Object, e As System.EventArgs) Handles bthDoWhile.Click Dim terms As Integer Dim index As Integer = 1 Dim sum As Integer = 0 If IsNumeric(txtInput.Text) Then terms = CInt(txtInput.Text) Else MessageBox.Show("Invalid value in the terms textbox") Exit Sub End If lstOut.Items.Add("Do While Loop") Do While index <= terms sum = sum + index ' MessageBox.Show("Adding term " & index & " current sum is " & sum) index = index + 1 Loop lstOut.Items.Add("The sum of integers 1 to " & terms & " is " & sum) End Sub Private Sub btnDoUntil_Click(sender As System.Object, e As System.EventArgs) Handles btnDoUntil.Click Dim terms As Integer Dim index As Integer = 1 Dim sum As Integer = 0 If IsNumeric(txtInput.Text) Then terms = CInt(txtInput.Text) Else MessageBox.Show("Invalid value in the terms textbox") Exit Sub End If lstOut.Items.Add("Do Until Loop") Do sum = sum + index ' MessageBox.Show("Adding term " & index & " current sum is " & sum) index = index + 1 Loop Until index > terms lstOut.Items.Add("The sum of integers 1 to " & terms & " is " & sum) End Sub Private Sub bthForNext_Click(sender As System.Object, e As System.EventArgs) Handles bthForNext.Click Dim terms As Integer Dim index As Integer = 1 Dim sum As Integer = 0 If IsNumeric(txtInput.Text) Then terms = CInt(txtInput.Text) Else MessageBox.Show("Invalid value in the terms textbox") Exit Sub End If lstOut.Items.Add("For Next Loop") For index = 1 To terms sum = sum + index Next lstOut.Items.Add("The sum of integers from 1 to " & terms & " is " & sum) End Sub Private Sub btnSumOdd_Click(sender As System.Object, e As System.EventArgs) Handles btnSumOdd.Click Dim index As Integer = 1 Dim terms As Integer = 0 Dim sum As Integer = 0 If IsNumeric(txtInput.Text) Then terms = CInt(txtInput.Text) Else MessageBox.Show("Invalid value in the terms textbox") Exit Sub End If For index = 1 To terms Step 2 sum = sum + index Next lstOut.Items.Add("The sum of the odd integers from 1 to " & terms & " is " & sum) End Sub Private Sub btnSumEven_Click(sender As System.Object, e As System.EventArgs) Handles btnSumEven.Click Dim index As Integer = 2 Dim terms As Integer = 0 Dim sum As Integer = 0 If IsNumeric(txtInput.Text) Then terms = CInt(txtInput.Text) Else MessageBox.Show("Invalid value in the terms textbox") Exit Sub End If For index = 2 To terms Step 2 sum = sum + index Next lstOut.Items.Add("The sum of the even integers from 2 to " & terms & " is " & sum) End Sub Private Sub btnSumFractions_Click(sender As System.Object, e As System.EventArgs) Handles btnSumFractions.Click Dim index As Integer = 1 Dim terms As Integer = 0 Dim sum As Double = 0 If IsNumeric(txtInput.Text) Then terms = CInt(txtInput.Text) Else MessageBox.Show("Invalid value in the terms textbox") Exit Sub End If For index = 1 To terms sum = sum + 1 / index Next lstOut.Items.Add("The sum of the fractions from 1 to 1/" & terms & " is " & FormatNumber(sum, 4)) End Sub Private Sub btnSquares_Click(sender As System.Object, e As System.EventArgs) Handles btnSquares.Click Dim index As Integer = 1 Dim terms As Integer = 0 Dim squares As Integer = 0 Dim line As String = "" If IsNumeric(txtInput.Text) Then terms = CInt(txtInput.Text) Else MessageBox.Show("Invalid value in the terms textbox") Exit Sub End If Do While index ^ 2 <= terms squares = index ^ 2 line = line & squares & " " index = index + 1 Loop lstOut.Items.Add(line) End Sub Private Sub btnSumSqFract_Click(sender As System.Object, e As System.EventArgs) Handles btnSumSqFract.Click Dim index As Integer = 1 Dim terms As Integer = 0 Dim sum As Double = 0 If IsNumeric(txtInput.Text) Then terms = CDbl(txtInput.Text) Else MessageBox.Show("Invalid value in the terms textbox") Exit Sub End If Do While index ^ 2 <= terms sum = sum + (1 / index) ^ 2 index = index + 1 Loop lstOut.Items.Add("The sum of the fractions squared from 1 to 1/" & terms & " is " & FormatNumber(sum, 4)) End Sub End Class