Suppose you save $100 each month into a savings account with the annual interest rate 5%. So, the monthly interest rate is 0.05/12 = 0.00417. After the first month, the value in the account becomes
100 * (1 + 0.00417) = 100.417

After the second month, the value in the account becomes
(100 + 100.417) * (1+0.00417) = 201.252

After the third month, the value in the account becomes,
(100 + 201.252) * (1 + 0.00417) = 302.507
and so on.
Write a program to display the account value after the sixth month.

Comments
I can fix that :(

Suppose you save $100 each month into a savings account with the annual interest rate 5%. So, the monthly interest rate is 0.05/12 = 0.00417. After the first month, the value in the account becomes
100 * (1 + 0.00417) = 100.417

After the second month, the value in the account becomes
(100 + 100.417) * (1+0.00417) = 201.252

After the third month, the value in the account becomes,
(100 + 201.252) * (1 + 0.00417) = 302.507
and so on.
Write a program to display the account value after the sixth month.

It seems to me that all you need is a for loop:

(100 + 0) * (1 + 0.00417) = 100.417
(100 + 100.417) * (1 + 0.00417) = 201.252
(100 + 201.252) * (1 + 0.00417) = 302.507

The (1 + 0.00417) repeats itself so you don't have to worry about it. Everything else is the same. The new result would be if you add to the old one the 100 and you multiply it by (1 + 0.00417).

result = (result+100)*a; Where a = (1 + 0.00417)

Just put that in a for loop.

Edited 6 Years Ago by javaAddict: n/a

Comments
I can fix that :)

Apparently giving good advices and suggestions on how to solve one's problem is a reason for down voting in this thread.
And I am referring to my previous post

Edited 6 Years Ago by javaAddict: n/a

Apparently giving good advices and suggestions on how to solve one's problem is a reason for down voting in this thread.
And I am referring to my previous post

Sorry About that im new to this website so i thought the down arrow meant to go to the next post and i was trying to undo it but don't know how. if you can let me know how ill undo that. Sorry once again and thank you for your help!

It seems to me that all you need is a for loop:


The (1 + 0.00417) repeats itself so you don't have to worry about it. Everything else is the same. The new result would be if you add to the old one the 100 and you multiply it by (1 + 0.00417).

result = (result+100)*a; Where a = (1 + 0.00417)

Just put that in a for loop.

thanks i fixed it

Sorry About that im new to this website so i thought the down arrow meant to go to the next post and i was trying to undo it but don't know how. if you can let me know how ill undo that. Sorry once again and thank you for your help!

Once someone votes a post they cannot vote again. If that was possible anyone could give thousands of positive or negative votes to anyone. That would cause a lot of mess.

you can do that without a loop. there is a formula I dont remember. however, here you have the code with the loop.

public class Interest{

public static void main(String[] args){
double x = 0;
for(int i = 1; i<4; i++){
x = (x +100) * 1.00417;
}

System.out.println(x);

}
}
Comments
The formula doesn't matter as the assignment was, probably, specifically for a loop. That said, what does the OP learn when you do the work.
Giving code is against the rules

that code is for three months. where you have four, put in 7, and you'll be told the amount after six months.

you can do that without a loop. there is a formula I dont remember. however, here you have the code with the loop.

public class Interest{

public static void main(String[] args){
double x = 0;
for(int i = 1; i<4; i++){
x = (x +100) * 1.00417;
}

System.out.println(x);

}
}

Giving code is against the rules especially when the poster said that the problem was solved

you can do that without a loop. there is a formula I dont remember. however, here you have the code with the loop.

public class Interest{

public static void main(String[] args){
double x = 0;
for(int i = 1; i<4; i++){
x = (x +100) * 1.00417;
}

System.out.println(x);

}
}

Thank You So Much!!!!!!!!

@JavaAddict:
I shall not give complete codes anymore. I did the same in another post and I was told not to do that anymore. accept my appolagy.
@Aisha:
you are welcome, although you won't get hat anymore from me :P

Hello, i have a different view of the problem you are solving. If it be that you are looking at a financial computation of the sort as how much money would have accumulated over a period, given monthly deposits of 100 dollars at a given annual interest rate i which is compounded monthly, quarterly, etc, then you would want to take a look at the formulas below:

I have no precise way to show the exponentiation operator, but whatever follows ^ in brackets is a power.

For monthly compounding:
A = 12p ( (1+i/m)^(mn) -1)/i where A = amount after n years, p is the principal you deposit every month, and m = 4 for quartely compounding or m = 12 for monthly compounding, etc, at the given annual interest i

For daily compounding:
A = p ( ( (1+i/m)^(mn) -1) )/( (1+i/m)^(m/12) -1)) here m = 360.

For continuous compounding:
A = p(( e^(in) -1 )/ (e^(i/12) - 1) )

In case you still have not solved your problem, then we shall begin from there.
Remember also to use BigDecimal not double types for your financial calculations.

Thanks.

Edited 6 Years Ago by kawalya: I have no precise way of showing the exponents as superscripts!!

In case you still have not solved your problem,

when the thread-starter says:

thanks i fixed it

it usually means: it's solved

import java.io.*;
class interest
{
 public static void main(String[] args)throws IOException
{
  BufferedReader kb=new BufferedReader(new InputStreamReader(System.in));
  double bal=0;
  int month,i;
  System.out.println("Enter number of months:");
  month=Integer.parseInt(kb.readLine());
  for(i=0;i<month;i++)
  {
    bal=(bal+100)*1.00417;
  }
  System.out.println("Balance after "+month+" months="+bal);
}
}

Complete code. Works for any number of months.

Can someone please close this thread? For the last week I've been hoping it would die, but it seems like every couple days somebody posts a new "solution" in here.

I agree with BJSJC, but I also realize that by posting here the thread will remain unclosed. So now that I have interjected my opinion, you may kill the thread :)

This article has been dead for over six months. Start a new discussion instead.