The output is: The amount at the end of year 11 is:52500.0
This amount is correct but why does it print year 11?
Also, how could I use this amount as the value of P for the second and so on till the 10th year?
I am a complete newbie in the Java world but please help.
Thanks alot

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39548

27

posted

0

That code doesn't even compile; are you sure it's the code that you're running? If you fix the syntax errors to make it compile, then it will indeed run from 1 to 10 for n. What's still missing is that the formula for "a" isn't what's mentioned in the description (and hence doesn't change for higher "n").

Hi Ulf & Priety,
Thanks for the comments. I have taken note.
Below is my refined code:

The ouput iterates ten times, incrementing 'n' but keeping same value of 'a'.
Kindly help me understand how I can increment 'n' first, before 'a' is calculated based on the formula.
Also, is there a way I could use the output when 'n=1' as the value of P when 'n=2' and so on till year 10?

You're calculating "a" only once - outside of the loop; you should move that statement into the loop.

Ogwal Boniface
Greenhorn

Joined: Oct 20, 2009
Posts: 7

posted

0

Hi Ulf,
Thanks alot. It works.
But one more thing, is there a way I could use the output when 'n=1' as the value of P when 'n=2' and so on till year 10?
Whatever your believe is, remained blessed...
Regards,
Ogwal

As far as I understand your requirement is like this:

Calculate the interest for 1 year on the beginning principal of 50000 using rate of interest as 5%.

Then for the 2nd year add the interest to the principal and recalculate the interest on it at 5% and so on for 10 years.

Is this right?

Then the calculation should show the following results:

The amount at the end of the 1 th year is:52500.0
The amount at the end of the 2 th year is:55125.0
The amount at the end of the 3 th year is:57881.2
The amount at the end of the 4 th year is:60775.3
The amount at the end of the 5 th year is:63814.0

When I ran your code the results were:

The amount at the end of the 1 th year is:52500.0
The amount at the end of the 2 th year is:57881.2
The amount at the end of the 3 th year is:67004.7
The amount at the end of the 4 th year is:81444.7
The amount at the end of the 5 th year is:103946.4

So is that code producing the right results?
Just curions.
Correct me if I am wrong.

It gave the correct results when I tried it. What are you doing differently?

Ogwal Boniface
Greenhorn

Joined: Oct 20, 2009
Posts: 7

posted

0

Hi Prietty,
Below is my complete code and the subsequent output:

My main problem is to use like the value of year1 in the output as the value of P for the second year and so on, giving something as below:
year 1 is 52,500.00
year 2 is 57,881.25
year 3 is 67,004.78
year 4 is 81,444.73
year 5 is 103,946.41
year 6 is 139,298.13
year 7 is 196,006.46
year 8 is 289,590.81
year 9 is 449,250.39
year 10 is 731,781.55
Grateful for further assistance
Regards

It gave the correct results when I tried it. What are you doing differently?

If its giving the correct results. Then its done.
If the formula mentioned in the spec. is just to be implemented in java.
I have no doubts then.
Thanks!