Results 1 to 5 of 5
 09182015, 12:47 PM #1Member
 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; // display headers 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 // display headers 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); } } }
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)
 09182015, 01:57 PM #2
Re: Can't find compound interest computation error
My output is wrong and I can't pinpoint where I went wrong.If you don't understand my response, don't ignore it, ask a question.
 09182015, 02:28 PM #3Member
 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
 09182015, 02:37 PM #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?If you don't understand my response, don't ignore it, ask a question.
 09182015, 03:26 PM #5Senior 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,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
Similar Threads

Trying to get a compound interest program to work
By gtorgers in forum New To JavaReplies: 6Last Post: 04102014, 02:07 PM 
Help Needed. New to Java Compound Interest Program....
By SubjectJ80 in forum New To JavaReplies: 7Last Post: 11052011, 09:15 AM 
Mean computation
By dienesh77 in forum New To JavaReplies: 7Last Post: 02222011, 12:00 AM 
Calculating compound interest using ONLY integers.
By AprilFlowers in forum New To JavaReplies: 2Last Post: 10212009, 06:47 AM 
Help with compound interest program pleaze
By Smirre in forum New To JavaReplies: 1Last Post: 01192009, 09:31 PM
Bookmarks