944,216 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 2892
  • Java RSS
Oct 20th, 2009
0

When to use Math.pow??

Expand Post »
Hi techies,
Kindly help me the situation below:

Java Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
easyb is offline Offline
8 posts
since Oct 2009
Oct 20th, 2009
0
Re: When to use Math.pow??
java Syntax (Toggle Plain Text)
  1. Math.pow(P*(1+r), n)
Reputation Points: 14
Solved Threads: 12
Junior Poster
ejosiah is offline Offline
119 posts
since Feb 2008
Oct 20th, 2009
0
Re: When to use Math.pow??
revision the P should be outside the Math.pow.
java Syntax (Toggle Plain Text)
  1. p * Math.pow((1+r),n);
next time look at the javadoc for usage info
Reputation Points: 14
Solved Threads: 12
Junior Poster
ejosiah is offline Offline
119 posts
since Feb 2008
Oct 20th, 2009
1
Re: When to use Math.pow??
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:

java Syntax (Toggle Plain Text)
  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. }
Reputation Points: 395
Solved Threads: 192
Veteran Poster
darkagn is offline Offline
1,136 posts
since Aug 2007
Oct 20th, 2009
0
Re: When to use Math.pow??
@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
Reputation Points: 14
Solved Threads: 12
Junior Poster
ejosiah is offline Offline
119 posts
since Feb 2008
Oct 20th, 2009
0

please help

Click to Expand / Collapse  Quote originally posted by ejosiah ...
@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
Reputation Points: 10
Solved Threads: 1
Newbie Poster
easyb is offline Offline
8 posts
since Oct 2009
Oct 20th, 2009
0

Result

Click to Expand / Collapse  Quote originally posted by darkagn ...
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:

java Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 1
Newbie Poster
easyb is offline Offline
8 posts
since Oct 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Need Suggestions on Final Year Project
Next Thread in Java Forum Timeline: Help displaying imageIcon in a frame





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC