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 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

Math.pow(P*(1+r), n)

revision the P should be outside the Math.pow.

p * Math.pow((1+r),n);

next time look at the javadoc for usage info

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:

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);
}

@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

@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

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:

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);
}

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.