| | |
When to use Math.pow??
![]() |
•
•
Join Date: Oct 2009
Posts: 3
Reputation:
Solved Threads: 0
Hi techies,
Kindly help me the situation below:
Any help is appreciated
Thanks,
Bonny
Kindly help me the situation below:
Java Syntax (Toggle Plain Text)
//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 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..**/ } }
Any help is appreciated
Thanks,
Bonny
0
#2 Oct 20th, 2009
java Syntax (Toggle Plain Text)
Math.pow(P*(1+r), n)
Knowledge is power but love conquers all
0
#3 Oct 20th, 2009
revision the P should be outside the Math.pow.
next time look at the javadoc for usage info
java Syntax (Toggle Plain Text)
p * Math.pow((1+r),n);
Knowledge is power but love conquers all
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:

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)
for(n=1; n<=10; n++) { P = P * (1+r); // a better solution would be to have r = 1.05 System.out.println("The amount at the end of the year " + n + " is:" + P); }
There are no stupid questions, only those too stupid to ask for help.
echo is a web developer's best friend. •
•
Join Date: Oct 2009
Posts: 3
Reputation:
Solved Threads: 0
•
•
•
•
@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
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
•
•
Join Date: Oct 2009
Posts: 3
Reputation:
Solved Threads: 0
•
•
•
•
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)
for(n=1; n<=10; n++) { P = P * (1+r); // a better solution would be to have r = 1.05 System.out.println("The amount at the end of the year " + n + " is:" + P); }
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
![]() |
Similar Threads
- Math.pow() problem... (Java)
- casting&math class in pow (C#)
- Using Math.pow() (Java)
- Issues using Math class (Java)
Other Threads in the Java Forum
- Previous Thread: java application that will print to patterns
- Next Thread: Help displaying imageIcon in a frame
| Thread Tools | Search this Thread |
2dgraphics 3d @param affinetransform android api apple applet application arc arguments array arrays automation banking binary binarytree bluetooth chatprogramusingobjects class client code color compare component count database derby design detection eclipse eclipsedevelopment encryption error fractal game givemetehcodez graphics gridlayout gui guitesting helpwithhomework html ide if_statement image integer interface j2me java java.xls javadesktopapplications javaprojects jni jpanel julia keytool keyword linux list macintosh map method methods mobile netbeans object open-source os pong problem producer program project projectideas property read recursion reference replaysolutions rim scanner server set size sms sort sql stop string swing terminal threads transforms tree ui unicode validation web windows





