Enjoy an ad free experience by logging in. Not a member yet? Register.

Results 1 to 4 of 4
Thread: When to use Math.pow??

10202009, 02:03 PM #1
 Join Date
 Oct 2009
 Posts
 1
 Thanks
 0
 Thanked 0 Times in 0 Posts
When to use Math.pow??
Hi techies,
Kindly help me the situation below:
//Interest.java
/*Mr. Brown invests $50,000.00 in a savings account yielding 5 percent interest.
* Assuming that all interest is left on deposit, write java codes to calculate and print
* the amount of money in the account at the end of each year for 10 years.
* Use the following formula for determining these amounts:
* a=P(1+r)n
* Where:
* P is the original amount invested i.e. the principal
* r is the annual interest rate
* n is the number of years
* a is the amount on deposit at the end of the nth year
* */
public class Interest {
public static void main(String[] args) {
double P=50000.00;
double r=0.05;
int n;
for(n=1; n<=10; n++);
System.out.println("The amount at the end of the first year is:" + P*(1+r));
/*this only prints the result for the first year but I would like to use this result as the principal for the second year and the subsequent for the 3rd year and so on..
**/
}
}
Any help is appreciated
Thanks,
Bonny
10202009, 06:52 PM
#2
 Join Date
 Sep 2009
 Posts
 18
 Thanks
 1
 Thanked 0 Times in 0 Posts
i'm not gonna show you in your code so that you can do this but here's your solution i think
save more coding and put in a while loop
(ex.)
That displays your n value, will display your amount and increments the n value all in one. and until the while value is true it keeps running whatever is under the do.Code:do System.out.println("the amount of " + n + " if you add 1 is " + ++n); while (n<=1000);
hope this helps
Last edited by mmcnitt; 10202009 at 07:10 PM.
10222009, 04:53 AM
#3
 Join Date
 Aug 2009
 Posts
 6
 Thanks
 3
 Thanked 0 Times in 0 Posts
Like what mmcnitt said.
But first, you should recheck your formula in your code with the given formula
10282009, 03:02 AM
#4
 Join Date
 Oct 2009
 Posts
 5
 Thanks
 0
 Thanked 0 Times in 0 Posts
for(n=1; n<=10; n++);
System.out.println("The amount at the end of the first year is:" + P*(1+r));
/*this only prints the result for the first year but I would like to use this result as the principal for the second year and the subsequent for the 3rd year and so on..
**/
Of course it always prints the result of the first year, your never changing the amount, your always printing P*(1+r).. Now, I am not sure about the formula but it looks like you'll probably need a variable to keep track of the current number and add the interest to it before printing:
for (n = 1; n <= 10; n++)
{
P += P*(1+r);
System.out.println(P);
}