Your figures don't really support what you want to do.  (Nor does your latest 
explanation, quite.)

Assuming you deposit your 1000 at the beginning of each year, and credit your interest 
at the end of the year ...

Year 1 = $1000 + interest = $1000 + 4.5% = $1045.00
Year 2 = year 1 + $1000 plus interest on all of that = $2045.00 + 4.5% = $2137.03
Year 3 = year 2 + $1000 plus interest on all of that = $3137.03 + 4.5% = $3278.19
Year 4 = year 3 + $1000 plus interest on all of that = $4278.19 + 4.5% = $4470.71

These figures do not match your original figures.  Three possible reasons for this: 
(1) my figures are rubbish (and that's quite possible); (2) your figures are wrong and 
don't represent the actual exercise; or (3) your figures are right and your 
explanation of the exercise is not.  (For instance, your figures work better with only 
two deposits of $1000 - although I don't get the same number of cents as you do, for 
some reason.)

Year 1 = $1000 + interest = $1000 + 4.5% = $1045.00
Year 2 = year 1 + $1000 plus interest on all of that = $2045.00 + 4.5% = $2137.03
Year 3 = year 2 + $0 plus interest on all of that = $2137.03 + 4.5% = $2233.19
Year 4 = year 3 + $0 plus interest on all of that = $2233.19 + 4.5% = $2333.68

You need a "for" loop to loop through your years and a "balance" variable.  It will 
start with a balance of zero.  First thing inside the loop you will make your deposit 
- i.e. you'll increase the balance by $1000.  That simulates the beginning of the 
year.  Then you'll add the interest - i.e. simulate the end of the year.

There are many ways of adding 4.5% interest to a balance, e.g.

  balance = balance + balance * 4.5 / 100;   // perhaps the most straightforward
  balance += balance * 4.5 / 100;  // make use of the += operator
  balance = balance * 1.045;  // use multiplication only
  balance *= 1.045;  // make use of the *= operator

For your initial training, I would suggest sticking with the first type of statement.

Dave S

  ----- Original Message ----- 
  From: Henrik Saxtorph 
  To: [EMAIL PROTECTED] 
  Sent: Friday, October 29, 2004 4:43 PM
  Subject: Re: [Java] Compound interest, I need help til the Java Code



  Thank you ffor your answer. Yes I want to make an new deposit og �1000 every year.
  Year 1 1000 + interest = Total amount
  Year 2 Total amount from year 1 + 1000 + interest of year 2's total amount

  Henrik

  David Smart <[EMAIL PROTECTED]> wrote:

  Depends on what you want to output.

  If you only require the final balance at the end of a number of years, then your 
equation is

  finalBalance = initialBalance * Math.pow (interestRate, numberOfYears);

  If you want each year separately, then you simply use a "for" loop to calculate it 
year by year, multiplying the balance for the year by 1 + 4.5% or 1.045.

  Note however, that your example numbers do not support this. The first four years 
should be

  $1000.00
  $1045.00
  $1092.03
  $1141.17

  not what you've got. Does this mean that you want to make an extra deposit during 
year 3?

  Dave S

  ----- Original Message ----- 
  From: exvisu 
  To: [EMAIL PROTECTED] 
  Sent: Friday, October 29, 2004 1:49 AM
  Subject: [Java] Compound interest, I need help til the Java Code




  I have got an exercise about compound interest.

  Year 1 is 1000 � deposit in the bank. 
  At the end of the year the deposit is increased with an interst of 
  4.5%. 
  Year 2 The balance is now 1045 � + 1000 * 4.5%
  Year 3 2137.07 * 4.5 %
  Year 4 2233.24

  How do I write this i Java

  I hope you can help me


  ---
  Outgoing mail is certified Virus Free.
  Checked by AVG anti-virus system (http://www.grisoft.com).
  Version: 6.0.786 / Virus Database: 532 - Release Date: 29/10/04

[Non-text portions of this message have been removed]



------------------------ Yahoo! Groups Sponsor --------------------~--> 
$9.95 domain names from Yahoo!. Register anything.
http://us.click.yahoo.com/J8kdrA/y20IAA/yQLSAA/5cFolB/TM
--------------------------------------------------------------------~-> 

If you have any comments or questions, submit it on the message board.
NO spam/ads/job posting or you will be BANNED from this group. Exception can be made 
it happen by notify me ahead of time.
all new members' message will be verified by me (spam..) before it get posted. 
Yahoo! Groups Links

<*> To reply to this message, go to:
    http://groups.yahoo.com/group/java_official/post?act=reply&messageNum=17307
    Please do not reply to this message via email. More information here:
    http://help.yahoo.com/help/us/groups/messages/messages-23.html

<*> To visit your group on the web, go to:  
    http://groups.yahoo.com/group/java_official/

<*> To unsubscribe from this group, send an email to:
    [EMAIL PROTECTED]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 



Reply via email to