Program to calculate compounded interest

I'm trying to create a program that will find the final balance using interest rates.
For example, if a person has an initial balance of $2.19 and its interest compounded monthly for 3 months.
To get the final balance, the formula would be:
[2.19 + ((1/12) * 2.19 * .03) + ((1/12) * 2.19 * .03) + ((1/12) * 2.19 * .03) + ((1/12) * 2.19 * .03)]

** the .03 is the interest rate (3%) **

As you notice "((1/12) * 2.19 * .03)" is used 3 times because it is being compounded monthly 3 times, this will be your total interest increase, then you add it to the initial balance which is 2.19.

((1/12) * 2.19 * .03) = ((1/12) * theInitialBalance * interestRate1)

Here is my attempt at this calculation, however, I'm getting no success.
1
2
3
4
5
6
7
calculateInt1 = ((1/12) * theInitialBalance * interestRate1);
for (int j = 1; j <= numberMonths; j++)
{
	calculateInt1 = calculateInt1 + calculateInt1;
}
finalBalance = calculateInt1 + theInitialBalance;
cout << finalBalance << endl;


I definied the ((1/12) * 2.19 * .03) as calculateInt1, then what I'm hoping I got right was the adding part inside the loop, I'm trying to make the ((1/12) * 2.19 * .03) + ((1/12) * 2.19 * .03) + ((1/12) * 2.19 * .03) + ((1/12) * 2.19 * .03). Then I would calculate the finalBalance by getting the total of calculateInt1 and adding that to the initialBalance (2.19).

However, this does not work, the output (finalBalance) is 2.19, it should be 2.21.

I'm just not 100% certain on the line within the for-loop, I feel like it is wrong, could someone explain what I should do instead?
Last edited on
Here is my new attempt, still no solution:

1
2
3
4
5
6
7
8
9
10
calculateInt1 = 0;

calculateIntHold = ((1/12) * theInitialBalance * interestRate1);
//calculateInt1 = ((1/12) * theInitialBalance * interestRate1);
for (int j = 1; j <= numberMonths; j++)
{
	calculateInt1 = calculateInt1 + calculateIntHold;
}
finalBalance = calculateInt1 + theInitialBalance;
cout << finalBalance << endl;
Your formula is wrong - google "compound interest formula" and read the wiki entry.

[Edit] - You should be getting a result of 2.39

Cheers
Jim
Last edited on
Okay, so the compounded formula from that Wiki page is:
A = P(1+(r/n))^(nt)
Where:
A = final amount
P = principal amount (initial investment)
r = annual interest rate
n = number of times the interest is compounded per year
t = number of years

If I were to make this be compounded monthly for x number of months, can I use this formula?

For example, using the numbers in my first post:
A = theInitialBalance(1+ (interestRate1/(1/12))^((1/12)(numberMonths))
A = 2.19(1 + (.03/(1/12))^((1/12)(4))

If that is correct, then I'm still not getting what I'm supposed to be getting.
However, ignoring the incorrect formula, have you declared calculateIntHold as a float or double, or an int? If you've used an int, any fractional result will be truncated down to the nearest whole number.

Jim
Let me explain the situation:

The interest is compounded monthly. This means a customer with a balance of $50.00 earns an interest of (1/12 * 3/100 * 50.00) dollars at the end of the month. This interest is added to the $50.00 and the sum becomes the current balance.
Okay, so the compounded formula from that Wiki page is:
A = P(1+(r/n))^(nt)


I'd be using this one:

FV = PV(1+i)^n

Which gives FV = 2.19(1+0.03)^3
Which is 2.19 * 1.092727
Let me explain the situation


Thanks, but I do understand what compound interest means.

Your equation is not taking into account the compounded element - it's just adding three times the first month's interest onto the initial balance.

Where did you get a figure of 2.21 from?

Jim
Well, I took the formula (1/12 * 3/100 * 2.19).
And did this: 2.19 + (1/12 * 3/100 * 2.19) + (1/12 * 3/100 * 2.19) + (1/12 * 3/100 * 2.19) + (1/12 * 3/100 * 2.19)

I used (1/12 * 3/100 * 2.19) in number times of months.

http://www.wolframalpha.com/input/?i=2.19+%2B+%281%2F12+*+3%2F100+*+2.19%29+%2B+%281%2F12+*+3%2F100+*+2.19%29+%2B+%281%2F12+*+3%2F100+*+2.19%29+%2B+%281%2F12+*+3%2F100+*+2.19%29

Which seems correct to me, or no?
OK, so 2.21 is the result of your equation, but your equation is wrong.

You need to fix your equation.

When you re-write, make sure you use double type variables, as I suspect you used an int before and that's why your summed interest terms were all zero (as per my previous post).

Cheers,
Jim
Okay so, let me make an algorithm of my new attempt;

Initial balance = 50.00;
Months = 4;

Initial balance = Initial balance + ((1/12) * .03 * Initial balance)
Then repeat 3 more times.

Correct?
Yeah, that looks to work.

Jim
Re-checking my earlier post (with the equation from wiki), I missed dividing the percent rate by 12 - doh!

So, the figures for that one should be
FV = 2.19(1+0.03/12)^3
= 2.19 * 1.007518765625
= 2.20646609671875

Which matches what your equation gives.

As an aside, I know you're using the for-loop method to try to understand code, but you should also consider how much more efficient (for large calculations) using the above formula would be over iterating your loop many many times.

Cheers,
Jim
Okay so yeah, that is correct, but now I'm getting different answers for examples of using more than 12 months.
For example, input 23 months and 95.04 initialBalance.

I used the formula to get 100.657, but the paper said it should be 100.83.
Is there a problem when it comes to using more than 12 months?
Last edited on
We're reaching the limit of my knowledge on compound interest here, but ...

Both your and wiki's formulas give 100.657 for 95.04, 3% and 23 months.

Now, plugging 100.83 into the second equation from wiki (that tells us PV for a given FV, 3%p.a. over 23 months) gives an initial balance of 95.20

Then plugging 95.20 into both your equation and the first wiki equation gives us 100.83 again, so there's a match between the two equations there.

I'd say that the example data you've been given has a mistake and should say 95.20 initial for a final of 100.83.

But I'm no expert and you should ask someone who is!

Jim
Once your at 100 or higher your in a different interest bracket. Did you account for that in your program?
Am I missing some vital information here? Like all the details of the problem? Would have been helpful, I reckon ;-)
Topic archived. No new replies allowed.