# Thread: Can't find compound interest computation error

1. Member
Join Date
Jan 2014
Posts
5
Rep Power
0

## Can't find compound interest computation error

I have this ridiculous assignment where I have to calculate compound interest in cents. Well, the instructions are:

Modify the application in Figure 5.6 to use only integers to calculate the compound interest. [Hint: Treat all monetary amounts as integral numbers of pennies. Then break the result into its dollars and cents portions by using the division and remainder operations, respectively. Insert a period between the dollars and the cents portions.

Here is the code for the aforementioned Figure 5.6:
Java Code:
```public class Interest {
public static void main(String[] args) {
double amt;
double principal = 1000;
double rate = 0.05;

System.out.printf("%s%20s\n", "Year", "Amount on deposit");

// calculate amt on deposit for each of 10 years
for (int year=1; year<=10; year++) {
// calculate amt for specified year
amt = principal * Math.pow(1.0+rate, year);
// display the year and amount
System.out.printf("%4d%,20.2f\n", year, amt);
}  // end for
}
}```

Here is my code:
Java Code:
```public class InterestInCents {
public static void main(String[] args) {
int amount;  // amt on deposit at end of each year in cents
double amtDouble;  // result of calculation; will be converted to int
int rate = 5;  // interest rate of 5 cents
int principal = 100000;  // one hundred thousand cents

System.out.println("\nCompound Interest in Cents");
System.out.printf("%s%20s\n", "Year", "Amount on Deposit");
// calculate amt on deposit for each of 10 years
for (int year=1; year<=10; year++) {
amtDouble =  principal * Math.pow(1+rate, year);
amount = (int) (Math.round(amtDouble));
System.out.printf("%4d%, 22d\n", year, amount);

} // end for
// Now break everything down to dollars
System.out.println("\nCompound Interest in Dollars\n");
System.out.printf("%s%20s\n", "Year", "Amount on Deposit");
for (int year=1; year<=10; year++) {
amtDouble = principal * Math.pow(1+rate, year);
amount = (int) (Math.round(amtDouble));
int dollars = amount / 100;
int cents = amount % 100;
// System.out.printf("%4d%, 20.2f\n", year, dollars + "." + cents);
System.out.println(year + "     " + dollars + "." + cents);
}
}
}```
My output is wrong and I can't pinpoint where I went wrong. I have never been good at math. Anyway, here are my results:
Java Code:
```Compound Interest in Cents
Year   Amount on Deposit
1               600,000
2             3,600,000
3            21,600,000
4           129,600,000
5           777,600,000
6           370,632,704
7        -2,071,171,072
8           457,875,456
9        -1,547,714,560
10          -696,352,768

Compound Interest in Dollars

Year   Amount on Deposit
1     6000.0
2     36000.0
3     216000.0
4     1296000.0
5     7776000.0
6     3706327.4
7     -20711710.-72
8     4578754.56
9     -15477145.-60
10     -6963527.-68
BUILD SUCCESSFUL (total time: 1 second)```
I hope someone here who is good at math can help me.

2. ## Re: Can't find compound interest computation error

My output is wrong and I can't pinpoint where I went wrong.

3. Member
Join Date
Jan 2014
Posts
5
Rep Power
0

## Re: Can't find compound interest computation error

The correct values are:

Year Amount on deposit
1 1,050.00
2 1,102.50
3 1,157.63
4 1,215.51
5 1,276.28
6 1,340.10
7 1,407.10
8 1,477.46
9 1,551.33
10 1,628.89

The values I get are:

Year Amount on Deposit
1 6000.0
2 36000.0
3 216000.0
4 1296000.0
5 7776000.0
6 3706327.4
7 -20711710.-72
8 4578754.56
9 -15477145.-60
10 -6963527.-68

4. ## Re: Can't find compound interest computation error

Take the output one line at a time and see why the code creates that value vs the correct value.
The code computes: 6000 vs 1050.
What is the computation that will compute 1050 given 1000 and 0.05?

5. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: Can't find compound interest computation error

Interest rate is stated in percent. So for 1000 to start with and a 10 percent rate, you get
1000*(1 + .10) = 1100 (return plus original amount). Since interest is compounded (interest on interest) you do it again.
1100*1.10 = 1210.

Or for N compounding periods you get

1000*(1.10)*(1.10)...N times

Regards,
Jim