When to use Math.pow??

Reply

Join Date: Oct 2009
Posts: 3
Reputation: easyb is an unknown quantity at this point 
Solved Threads: 0
easyb easyb is offline Offline
Newbie Poster

When to use Math.pow??

 
0
  #1
Oct 20th, 2009
Hi techies,
Kindly help me the situation below:

  1. //Interest.java
  2. /*Mr. Brown invests $50,000.00 in a savings account yielding 5 percent interest.
  3.  * Assuming that all interest is left on deposit, write java codes to calculate and print
  4.  * the amount of money in the account at the end of each year for 10 years.
  5.  * Use the following formula for determining these amounts:
  6.  * a=P(1+r)n
  7.  * Where:
  8.  * P is the original amount invested i.e. the principal
  9.  * r is the annual interest rate
  10.  * n is the number of years
  11.  * a is the amount on deposit at the end of the nth year
  12.  * */
  13.  
  14. public class Interest {
  15.  
  16. public static void main(String[] args) {
  17. double P=50000.00;
  18. double r=0.05;
  19. int n;
  20. for(n=1; n<=10; n++);
  21.  
  22. System.out.println("The amount at the end of the first year is:" + P*(1+r));
  23.  
  24. /*this only prints the result for the first year and I would like to use this result as the principal for the second year and the subsequent for the 3rd year and so on..**/
  25.  
  26. }
  27.  
  28. }

Any help is appreciated
Thanks,
Bonny
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 112
Reputation: ejosiah is an unknown quantity at this point 
Solved Threads: 12
ejosiah's Avatar
ejosiah ejosiah is offline Offline
Junior Poster
 
0
  #2
Oct 20th, 2009
  1. Math.pow(P*(1+r), n)
Knowledge is power but love conquers all
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 112
Reputation: ejosiah is an unknown quantity at this point 
Solved Threads: 12
ejosiah's Avatar
ejosiah ejosiah is offline Offline
Junior Poster
 
0
  #3
Oct 20th, 2009
revision the P should be outside the Math.pow.
  1. p * Math.pow((1+r),n);
next time look at the javadoc for usage info
Knowledge is power but love conquers all
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 790
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 109
darkagn's Avatar
darkagn darkagn is offline Offline
Master Poster
 
1
  #4
Oct 20th, 2009
Hi easyb and welcome to DaniWeb

You don't need to use Math.pow at all. Math.pow is used to find the power of a number. I wish that banks would pay interest exponentially but unfortunately...

I suggest you revise your notes on iteration and for-loops. I think what you want would look something like this:

  1. for(n=1; n<=10; n++) {
  2. P = P * (1+r); // a better solution would be to have r = 1.05
  3. System.out.println("The amount at the end of the year " + n + " is:" + P);
  4. }
There are no stupid questions, only those too stupid to ask for help.
echo is a web developer's best friend.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 112
Reputation: ejosiah is an unknown quantity at this point 
Solved Threads: 12
ejosiah's Avatar
ejosiah ejosiah is offline Offline
Junior Poster
 
0
  #5
Oct 20th, 2009
@darkagn I know there are several ways of achieving the same result but I think it will be better for easyb to stay inline with the question and use the formula as proposed
Knowledge is power but love conquers all
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 3
Reputation: easyb is an unknown quantity at this point 
Solved Threads: 0
easyb easyb is offline Offline
Newbie Poster

please help

 
0
  #6
Oct 20th, 2009
Originally Posted by ejosiah View Post
@darkagn I know there are several ways of achieving the same result but I think it will be better for easyb to stay inline with the question and use the formula as proposed
Hi Ejosiah,
Kindly advice on how best i can start off with this problem. With the code above, I can only get the value of the first year but I like to use the value of the first year as the initial value of P for the 2nd year and so on...
Thanks alot
Easyb
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 3
Reputation: easyb is an unknown quantity at this point 
Solved Threads: 0
easyb easyb is offline Offline
Newbie Poster

Result

 
0
  #7
Oct 20th, 2009
Originally Posted by darkagn View Post
Hi easyb and welcome to DaniWeb

You don't need to use Math.pow at all. Math.pow is used to find the power of a number. I wish that banks would pay interest exponentially but unfortunately...

I suggest you revise your notes on iteration and for-loops. I think what you want would look something like this:

  1. for(n=1; n<=10; n++) {
  2. P = P * (1+r); // a better solution would be to have r = 1.05
  3. System.out.println("The amount at the end of the year " + n + " is:" + P);
  4. }
Hi Darkagn,
Using that code returns:
"The amount at the end of the year 11 is 52500.0"
This is a good result though but how can i get this value of year 1 to print out an amount for the 2nd year and so on upto to the 10th year.
I am stuck here please. I am only a month old into Java.
Thanks,
Easyb
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC