# Doubling Time, do loops

Page 1 of 1

## 4 Replies - 1624 Views - Last Post: 10 July 2014 - 04:36 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=350082&amp;s=52ffbbc6e7f71e81ea88a15191f4aae0&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 stinkytotoro

Reputation: 0
• 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

• blow up my boots

Reputation: 6541
• Posts: 26,470
• 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.

### #3 stinkytotoro

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

## Re: Doubling Time, do loops

Posted 09 July 2014 - 10:20 PM

andrewsw, 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

### #4 stinkytotoro

Reputation: 0
• 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

### #5 dbasnett

Reputation: 118
• Posts: 651
• 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)

```