Results 1 to 5 of 5
  1. #1
    stupidmoron is offline Member
    Join Date
    Jan 2014
    Posts
    5
    Rep Power
    0

    Default 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);
        }
      }
    }
    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. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Can't find compound interest computation error

    My output is wrong and I can't pinpoint where I went wrong.
    Please explain. Add some comments to your post to show the wrong values and post the correct values.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    stupidmoron is offline Member
    Join Date
    Jan 2014
    Posts
    5
    Rep Power
    0

    Default 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. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default 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.

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

    Default 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
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Trying to get a compound interest program to work
    By gtorgers in forum New To Java
    Replies: 6
    Last Post: 04-10-2014, 02:07 PM
  2. Help Needed. New to Java Compound Interest Program....
    By SubjectJ80 in forum New To Java
    Replies: 7
    Last Post: 11-05-2011, 09:15 AM
  3. Mean computation
    By dienesh77 in forum New To Java
    Replies: 7
    Last Post: 02-22-2011, 12:00 AM
  4. Calculating compound interest using ONLY integers.
    By AprilFlowers in forum New To Java
    Replies: 2
    Last Post: 10-21-2009, 06:47 AM
  5. Help with compound interest program pleaze
    By Smirre in forum New To Java
    Replies: 1
    Last Post: 01-19-2009, 09:31 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •