wood burning stoves 2.0*
The moose likes Java in General and the fly likes Use of Math.pow? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Use of Math.pow?" Watch "Use of Math.pow?" New topic
Author

Use of Math.pow?

Ogwal Boniface
Greenhorn

Joined: Oct 20, 2009
Posts: 7
Hi techies,
Kindly help me the situation below:



Any help is appreciated
Thanks,
Bonny


~~Great minds read, ask and then share~~
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41182
    
  45
Start by introducing the "a" variable. It needs to increase in each iteration.


Ping & DNS - my free Android networking tools app
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18553
    
  40

Not completely sure what you are asking... but why not? Sure, the Math.pow() method can be used here to raise the percentage to the number of years.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ogwal Boniface
Greenhorn

Joined: Oct 20, 2009
Posts: 7
Hi,
I am particularly interested in the code that can make solve that problem based on the formula given and the scenario as a whole.

But Ulf, how can I introduce the 'a' variable to make the iteration?

Thanks
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41182
    
  45
For starters, the loop needs to change. "for(n=1; n<=10; n++);" does nothing; instead, use something like
Ogwal Boniface
Greenhorn

Joined: Oct 20, 2009
Posts: 7
Hi Ulf,
Thanks alot for replying.
Your code:

What I did:

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: 41182
    
  45
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").
Priety Sharma
Ranch Hand

Joined: Jun 10, 2008
Posts: 156

[ UD: Thanks for your contribution, but we don't want to rob Ogwal of an opportunity to learn: JavaRanch is NotACodeMill ]


Priety.
Priety Sharma
Ranch Hand

Joined: Jun 10, 2008
Posts: 156

Ulf,

I realised it.
Thanks.
Ogwal Boniface
Greenhorn

Joined: Oct 20, 2009
Posts: 7
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?

**Priety**..I understand the meaning of JavaRanch is 'NotACodeMill http://faq.javaranch.com/java/NotACodeMill' but I am trying to come up atleast with something as per what I know so far...

Thanks again for the time. It's appreciated
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41182
    
  45
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
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
Priety Sharma
Ranch Hand

Joined: Jun 10, 2008
Posts: 156

Hi Ogwal,

Good that you have achieved what you were trying to do.

Priety Sharma
Ranch Hand

Joined: Jun 10, 2008
Posts: 156

Hi Ogwal,

I have a doubt.

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.


Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38107
    
  22
It gave the correct results when I tried it. What are you doing differently?
Ogwal Boniface
Greenhorn

Joined: Oct 20, 2009
Posts: 7
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
Priety Sharma
Ranch Hand

Joined: Jun 10, 2008
Posts: 156

Hi,

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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Use of Math.pow?
 
Similar Threads
Generating compound interest with integers
New to ArrayList
I need to have my program take information from text file Plus my pie chart is in wrong place
extra credit in class but stuck on persentage rates
Did I do this right? (Interest calculator)