Peter Haugen

Greenhorn

Posts: 12

posted 4 years ago

Sure. Assuming you want compound interest (which is what it looks like) you need to recalculate '

Winston

Valley Kayak wrote:Any one have any helpful hints?

Sure. Assuming you want compound interest (which is what it looks like) you need to recalculate '

`nextmonth`' and

*update the principal*for each iteration of the loop. You're doing neither.

Winston

"Leadership is nature's way of removing morons from the productive flow" - Dogbert

Articles by Winston can be found here

Peter Haugen

Greenhorn

Posts: 12

posted 4 years ago

You are correct, I am trying to determine compound interest. I understand that I need to recalculate the monthly amount each time. At this point the program calculates the first months interest and then just applies that to the rest of the months. It seems like the formula should grow for each iteration of the loop. For example, the first month would be " initialAmount + initialAmount * (rate / 1200) " = secondMonth. Then the third month would look like " secondMonth + secondMonth * (rate / 1200) " = third month...... until it reaches the end of the loop. I guess that I am really stuck on how to adjust my formula to take into account the growth from the first month, second month and on through the loop.

I also tried just entering the compound interest formula, but would that work inside the loop? Also, I had problems with Math.pow and ended up with much larger values than what I needed.

Winston Gutkowski wrote:Valley Kayak wrote:Any one have any helpful hints?

Sure. Assuming you want compound interest (which is what it looks like) you need to recalculate 'nextmonth' andupdate the principalfor each iteration of the loop. You're doing neither.

Winston

You are correct, I am trying to determine compound interest. I understand that I need to recalculate the monthly amount each time. At this point the program calculates the first months interest and then just applies that to the rest of the months. It seems like the formula should grow for each iteration of the loop. For example, the first month would be " initialAmount + initialAmount * (rate / 1200) " = secondMonth. Then the third month would look like " secondMonth + secondMonth * (rate / 1200) " = third month...... until it reaches the end of the loop. I guess that I am really stuck on how to adjust my formula to take into account the growth from the first month, second month and on through the loop.

I also tried just entering the compound interest formula, but would that work inside the loop? Also, I had problems with Math.pow and ended up with much larger values than what I needed.

posted 4 years ago

And that's because you''re not

Winston

Valley Kayak wrote:I guess that I am really stuck on how to adjust my formula to take into account the growth from the first month, second month and on through the loop.

And that's because you''re not

*updating*the principal (which you've unfortunately called '

`initialAmount`'). Try and think how you would do that

*inside*your loop.

Winston

Articles by Winston can be found here

Campbell Ritchie

Sheriff

Posts: 53779

128

Peter Haugen

Greenhorn

Posts: 12

posted 4 years ago

Alright, so I started over and tried a new tactic. At this point I am getting values that are very close to being correct (horse shoes and hand grenades< i know.) The problem now is that I seem to be having rounding issues. The first value is off by .04 and the other values pretty much follow this trend. I am testing the program at $10000, 5.75%, and 18 months. My first value is 10047. 95 vs the correct value of 10047.91 and the final value outputs at 10899.28 vs a correct value of 10898.54. Would this be a formatting difference?

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

double principal = 0; //Starting amount entered by user

double interestRate = 0; // interest rate as ***% entered by user

double numberOfMonths = 0; //Entered by the user

double amount; //total after the time period.

//User enters principal, interest rate, and number of months.

//Principal

System.out.print("Enter starting amount :$ ");

principal = input.nextDouble();

//Interest Rate

System.out.print("Enter interest rate: ");

interestRate = input.nextDouble() / 100.0;

//Number of Months

System.out.print("Enter time period: ");

numberOfMonths = input.nextDouble();

for(int month = 1 ; month <= numberOfMonths ; month++){

amount = principal * Math.pow(1 + interestRate / numberOfMonths, (numberOfMonths / 12) * month);

System.out.println("After month # " + month + " The amount in the CD is $ " + amount);

}

}

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

double principal = 0; //Starting amount entered by user

double interestRate = 0; // interest rate as ***% entered by user

double numberOfMonths = 0; //Entered by the user

double amount; //total after the time period.

//User enters principal, interest rate, and number of months.

//Principal

System.out.print("Enter starting amount :$ ");

principal = input.nextDouble();

//Interest Rate

System.out.print("Enter interest rate: ");

interestRate = input.nextDouble() / 100.0;

//Number of Months

System.out.print("Enter time period: ");

numberOfMonths = input.nextDouble();

for(int month = 1 ; month <= numberOfMonths ; month++){

amount = principal * Math.pow(1 + interestRate / numberOfMonths, (numberOfMonths / 12) * month);

System.out.println("After month # " + month + " The amount in the CD is $ " + amount);

}

}

posted 4 years ago

First mistake.

You were already told what your problem was, but instead of

p = (1+r)^n formula; and unfortunately, you appear to have got it wrong.

Specifically:

My advice: Go back and get your old program working first.

Winston

Valley Kayak wrote:Alright, so I started over and tried a new tactic.

First mistake.

You were already told what your problem was, but instead of

*solving*it, you've completely rewritten your program to use a different set of logic, ie, the

p = (1+r)^n formula; and unfortunately, you appear to have got it wrong.

Specifically:

`interestRate / numberOfMonths`will only work if

`numberOfMonths`is 12 (I assume that

`interestRate`is an

*annual*interest rate and that this is a monthly calculation).

My advice: Go back and get your old program working first.

*Then*start on your new one.

Winston

Articles by Winston can be found here

Peter Haugen

Greenhorn

Posts: 12

posted 4 years ago

I am back on the old one...

I thought that in your earlier recommendation, where you linked me to the wiki page on compound interest, you were suggesting that I try to use that formula...Regardless,

I have tried what seems like 50 different things and nothing seems to be working . I know that I need to update the principal after each loop (on my original program)I just don't know how to do that. I am sorry if this is like screaming at a wall for you, but I have been trying different formulas all morning and nothing is working for me.

Winston Gutkowski wrote:Valley Kayak wrote:Alright, so I started over and tried a new tactic.

My advice: Go back and get your old program working first.Thenstart on your new one.

Winston

I am back on the old one...

I thought that in your earlier recommendation, where you linked me to the wiki page on compound interest, you were suggesting that I try to use that formula...Regardless,

I have tried what seems like 50 different things and nothing seems to be working . I know that I need to update the principal after each loop (on my original program)I just don't know how to do that. I am sorry if this is like screaming at a wall for you, but I have been trying different formulas all morning and nothing is working for me.

posted 4 years ago

Oh, sorry. It was simply an info link to make sure that we were both talking about the same thing.

OK. If you want to

So, if I want to add 10 to a value and make sure it sticks, I need to write:

HIH

Winston

- 1

Peter Haugen wrote:I thought that in your earlier recommendation, where you linked me to the wiki page on compound interest, you were suggesting that I try to use that formula...

Oh, sorry. It was simply an info link to make sure that we were both talking about the same thing.

I have tried what seems like 50 different things and nothing seems to be working. I know that I need to update the principal after each loop (on my original program)I just don't know how to do that.

OK. If you want to

*update*a value, you need to

*reassign*it.

So, if I want to add 10 to a value and make sure it sticks, I need to write:

`x`

__=__x + 10;HIH

Winston

Articles by Winston can be found here

Peter Haugen

Greenhorn

Posts: 12

posted 4 years ago

I got it finally...

for (int i = 1; i <= numbMonths; i++){

System.out.println("Month " + i );

principal = principal + principal * interestRate / 1200;

System.out.println(principal);

Thanks for all of your help... I am sorry that it has taken me so long, but I was trying to do that for hours and for some reason it just worked. Obviously it worked because I did it correctly unlike the ~150 other tries.

This is really a great site and I hope that I can contribute more in the future...

for (int i = 1; i <= numbMonths; i++){

System.out.println("Month " + i );

principal = principal + principal * interestRate / 1200;

System.out.println(principal);

Thanks for all of your help... I am sorry that it has taken me so long, but I was trying to do that for hours and for some reason it just worked. Obviously it worked because I did it correctly unlike the ~150 other tries.

This is really a great site and I hope that I can contribute more in the future...

posted 4 years ago

Thanks for the kudos, but the fact is, it didn't "just" work; it worked because you were doing it

Well done.

Winston

Peter Haugen wrote:Thanks for all of your help... I am sorry that it has taken me so long, but I was trying to do that for hours and for some reason it just worked. Obviously it worked because I did it correctly unlike the ~150 other tries.

This is really a great site and I hope that I can contribute more in the future...

Thanks for the kudos, but the fact is, it didn't "just" work; it worked because you were doing it

**correctly**.

Well done.

Winston

Articles by Winston can be found here

Junaid Mahmud

Ranch Hand

Posts: 118

posted 6 months ago

Hello! I am currently having issues with this java problem. Please help. Here is what I have so far. The problem I am having is that it wont print out the statement in the for loop. Thanks!

Piet Souris

Rancher

Posts: 1783

55

posted 6 months ago

hi Junaid,

have a look at line 10, and have a look at the variables 'noMonth' and the variable you use in your for-loop. Notice anything?

Also: when you request an interestRate, give an indication what interestRate your program is expecting. Is that an interestrate as a percentage, or as a perunage (i.e. should one enter 10, or 0.1), and per what time period this is valid. I see what you mean in the code, but the question itself doesn't make that clear.

have a look at line 10, and have a look at the variables 'noMonth' and the variable you use in your for-loop. Notice anything?

Also: when you request an interestRate, give an indication what interestRate your program is expecting. Is that an interestrate as a percentage, or as a perunage (i.e. should one enter 10, or 0.1), and per what time period this is valid. I see what you mean in the code, but the question itself doesn't make that clear.

Junaid Mahmud

Ranch Hand

Posts: 118

posted 6 months ago

This is the updated version. I keep on getting on the same output for each month as well as the calculations are off. The first 4 months should be something like this.

Amount after one month the compound interest is: 100.41666666666667

Amount after two months the compound interest: 100.83333333333334

Amount after three months the compound interest: 101.25000000000001

Amount after four months the compound interest: 101.66666666666669

Amount after one month the compound interest is: 100.41666666666667

Amount after two months the compound interest: 100.83333333333334

Amount after three months the compound interest: 101.25000000000001

Amount after four months the compound interest: 101.66666666666669

Junaid Mahmud

Ranch Hand

Posts: 118

posted 6 months ago

Update! I am almost there I am just a little off and have no clue why. Here is the update code. The first four months must have the output of these numbers.

Amount after one month the compound interest is: 100.41666666666667

Amount after two months the compound interest: 100.83333333333334

Amount after three months the compound interest: 101.25000000000001

Amount after four months the compound interest: 101.66666666666669

Amount after one month the compound interest is: 100.41666666666667

Amount after two months the compound interest: 100.83333333333334

Amount after three months the compound interest: 101.25000000000001

Amount after four months the compound interest: 101.66666666666669

Piet Souris

Rancher

Posts: 1783

55

posted 6 months ago

Hmm, it is not the inaccuracy of the floating point system...

First of all: you did not follow my recommendation to be specific when you ask for the interestRate. So, again, is it the interest per $1,- (called 'perunage)', or per $100,- (called percentage)? And is that a rate per year or per time period?

Next, look at the part where you calculate the principal. Have a very good look, and tell me whether you think the code is correct:

Where do you update your principal? In fact you don't. You update the variable 'endingAmount'. But do you update this amount with the interestrate per period, or the interestrate per year?

First of all: you did not follow my recommendation to be specific when you ask for the interestRate. So, again, is it the interest per $1,- (called 'perunage)', or per $100,- (called percentage)? And is that a rate per year or per time period?

Next, look at the part where you calculate the principal. Have a very good look, and tell me whether you think the code is correct:

Where do you update your principal? In fact you don't. You update the variable 'endingAmount'. But do you update this amount with the interestrate per period, or the interestrate per year?

Piet Souris

Rancher

Posts: 1783

55

posted 6 months ago

Hmm, to be frank, I am a little cryptic, I could be more clear. So:

Suppose the interest rate is i per year, and that interest is granted on a monthly basis. Now, there are basically two ways to calculate the monthly interest:

1 we define the interest per month as (1 + i) ^ (1/12) - 1

2 we define the interest per month as i / 12

According to 1), after 5 months our principal of 1 has become (1 + i) ^ (5 / 12)

According to 2), our principal is now (1 + i/12) ^ 5.

Both methods are not equal. You have to decide upfront which method is to be used. If you get different answers than your textbook, then this is very likely the source of the difference.

For the interested reader: if we follow method 2, and we make the time period ever smaller (so that we can speak of continuous interest), what happens to the principal after n years? And according to method 1?

Suppose the interest rate is i per year, and that interest is granted on a monthly basis. Now, there are basically two ways to calculate the monthly interest:

1 we define the interest per month as (1 + i) ^ (1/12) - 1

2 we define the interest per month as i / 12

According to 1), after 5 months our principal of 1 has become (1 + i) ^ (5 / 12)

According to 2), our principal is now (1 + i/12) ^ 5.

Both methods are not equal. You have to decide upfront which method is to be used. If you get different answers than your textbook, then this is very likely the source of the difference.

For the interested reader: if we follow method 2, and we make the time period ever smaller (so that we can speak of continuous interest), what happens to the principal after n years? And according to method 1?

Junaid Mahmud

Ranch Hand

Posts: 118

posted 6 months ago

This is the updated version. And this is the output that I am currently giving.

Enter initial savings amount: $100

Enter interest rate: 0.05

Enter time period: 2

Month 1

100.41666666666667

Month 2

100.41666666666667

However month 2 should be such as

Amount after one month the compound interest is: 100.41666666666667

Amount after two months the compound interest: 100.83333333333334

Amount after three months the compound interest: 101.25000000000001

Amount after four months the compound interest: 101.66666666666669

Enter initial savings amount: $100

Enter interest rate: 0.05

Enter time period: 2

Month 1

100.41666666666667

Month 2

100.41666666666667

However month 2 should be such as

Amount after one month the compound interest is: 100.41666666666667

Amount after two months the compound interest: 100.83333333333334

Amount after three months the compound interest: 101.25000000000001

Amount after four months the compound interest: 101.66666666666669

Junaid Mahmud

Ranch Hand

Posts: 118

Junaid Mahmud

Ranch Hand

Posts: 118

posted 6 months ago

I have almost figured it out but need help on one thing. If the input is 1 month the output should be 100.416, but It is displaying 0.0 which makes no sense because all other outputs are correct. If I change the value of the variable then everything changes. Please help me out. Thanks! I also started over!

Piet Souris

Rancher

Posts: 1783

55

Don't get me started about those stupid light bulbs. |