4 Replies - 329 Views - Last Post: 10 July 2014 - 04:36 AM Rate Topic: -----

#1 stinkytotoro  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 31-May 14

Doubling Time, do loops

Posted 09 July 2014 - 09:50 PM

Write a program to illustrate the growth of money in a savings account. When the user10000 presses the button, values for Amount and Interest Rate (in percent) are obtained from text boxes and used to calculate the number of years until the money doubles and the number of years until the money reaches a million dollars. A sample interface has been shown below. (Use Do loops)

  Private Sub btnDetermine_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDetermine.Click
        Dim amount As Double
        amount = CDbl(txtAmount.Text)
        Dim interest As Double
        interest = CDbl(txtInterest.Text) / 100
        Dim newbalance As Double = amount
        Dim years As Integer = 0
        Dim millionyears As Integer


        Do While amount < 100000
            amount += interest * amount
            millionyears += 1
            txtMillion.Text = millionyears
        Loop


        Do Until newbalance >= amount * 2
            newbalance = newbalance * (1 + interest)
            years += 1
        Loop
        txtDoublingTime.Text = years
    End Sub




Got correct output for million years, unsure of what I am doing wrong for the doubling time loop getting incorrect output

Is This A Good Question/Topic? 0
  • +

Replies To: Doubling Time, do loops

#2 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3738
  • View blog
  • Posts: 13,067
  • Joined: 12-December 12

Re: Doubling Time, do loops

Posted 09 July 2014 - 10:17 PM

What is "incorrect output"?

Explain what values you put and in, what values you get out, and what values you should be getting.
Was This Post Helpful? 0
  • +
  • -

#3 stinkytotoro  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 31-May 14

Re: Doubling Time, do loops

Posted 09 July 2014 - 10:20 PM

View Postandrewsw, on 09 July 2014 - 10:17 PM, said:

What is "incorrect output"?

Explain what values you put and in, what values you get out, and what values you should be getting.


I put in
amount- $10 000
interest- 6%

doubling time should be - 12 but comes out 52
million years-40 which is correct
Was This Post Helpful? 0
  • +
  • -

#4 stinkytotoro  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 31-May 14

Re: Doubling Time, do loops

Posted 09 July 2014 - 11:32 PM

Public Class frmDetermineYears
    Private Sub btnDetermine_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDetermine.Click
        Dim amount As Double = CDbl(txtAmount.Text)
        Dim newbalance As Double = amount
        Dim interest As String = txtInterest.Text
        Dim m As Integer = interest.IndexOf("%")
        interest = interest.Substring(0, m)
        interest = CStr(CDbl(interest) / 100)
        Dim years As Integer
        Dim millionyears As Integer

        Do Until (newbalance >= amount * 2) 'doubling time
            newbalance = newbalance * (CDbl(interest) + 1)
            years += 1
        Loop
        txtDoublingTime.Text = CStr(years)

        Do While amount <= 100000 'reach a million years
            amount += CDbl(interest) * amount
            millionyears += 1
            txtMillion.Text = CStr(millionyears)
        Loop
    End Sub
End Class


yikes all I had to do was switch the loops/ working
Was This Post Helpful? 0
  • +
  • -

#5 dbasnett  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 109
  • View blog
  • Posts: 608
  • Joined: 01-October 08

Re: Doubling Time, do loops

Posted 10 July 2014 - 04:36 AM

I came up with a different answer. Double in 11 years, million in 77 years.

        Dim initAmt As Decimal = 10000D
        Dim dblAmt As Decimal = initAmt * 2D
        Dim dblMonths As Integer = -1
        Dim ctMonths As Integer = 0
        Dim yrRate As Decimal = 0.06D

        'assume the money is compunded monthly
        Dim moRate As Decimal = yrRate / 12D

        Do While initAmt < 1000000D
            If dblMonths = -1 AndAlso initAmt >= dblAmt Then
                dblMonths = ctMonths
            End If
            ctMonths += 1
            initAmt += initAmt * moRate
        Loop
        Debug.WriteLine("DBL in " & (dblMonths / 12D).ToString)
        Debug.WriteLine("Mil in " & (ctMonths / 12D).ToString)


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1