27 April, 2011

Repetition



1.            Compute the sum of the following series:
(a)  1+2+3+………. +N, where N is input and N > 1.

Dim i As Integer = 1
Dim output As Integer = 0
For i = 1 To TextBox1.Text
output = output + i
TextBox2.Text = output
Next i
End Sub


(b)  1+3+5+………..+ (2N+1), where N is input and N > 0.

Dim i As Integer = 1
Dim output As Integer = 0
Do While i <= 2 * TextBox1.Text + 1
output = output + i
i = i + 2
Loop
TextBox2.Text = output
End Sub


(c)   2+4+6+………..+ (2N), where N is input and N > 1.

Dim i As Integer = 2
Dim output As Integer = 0
Do While i <= 2 * TextBox1.Text
output = output + i
i = i + 2
Loop
MsgBox(output)
End Sub


(d)  52+102+152+……. + (5N) 2, where N is input and N > 1.

Dim s As Integer=0
Dim i As Integer=1
Dim m As Integer
Dim n As Integer
i=TextBox1.Text
n=TextBox1.Text
Do While i<=n
m=i*i
i=i+5
Loop
TextBox3.Text=s
End Sub


2.            Calculate the average of numbers within a range. (For example, average of numbers from 1 to 100). The range will be given as input.

     Dim i As Integer = TextBox1.Text
     Dim output As Integer = 0
     Do While i <= TextBox2.Text
     output = output + i
     i = i + 1
     Loop
     TextBox3.Text = output / TextBox2.Text
     End Sub


3.            Calculate the total number of evens or odds within a range of numbers (for example, the total number of evens or odds from 3 to 30). The range will be given as input.

          Dim i As Integer = TextBox1.Text
      Dim s As Integer = 0
      Dim a As Integer = 0
      For i = TextBox1.Text To TextBox2.Text
      If i Mod 2 = 0 Then
      s = s + 1
      Else
      a = a + 1
      End If
      Next i
      TextBox3.Text = s
      TextBox4.Text = a
      End Sub

4.            Calculate the factorial of a given number.
         
          Dim i As Integer = 1
      Dim output As Integer = 1
      Do While i <= TextBox1.Text
      output = output * i
      i = i + 1
      Loop
      TextBox2.Text = output
      End Sub

5.            Calculate the number of years to become a millionaire if a particular amount of money is deposited in a savings account and let it accumulate at a specific interest rate compounded annually.
         
        Dim a As Integer = TextBox1.Text
      Dim i As Integer = TextBox2.Text
      Dim c As Integer = 0
      Do While (a <= 100000)
      a = a + (a * i / 100)
      c = c + 1
      Loop
      TextBox3.Text = c
      End Sub

0 comments:

Post a Comment