Thread: Compound Interest?

  1. #1
    Registered User
    Join Date
    Feb 2011
    Location
    San Diego
    Posts
    10

    Compound Interest?

    I have been trying for many hours to get this to work, but alas no success. i have about 6 programs to write and they all build off of this one so I'm stuck. I could really use some help please. this is my first post btw in case I am missing some sort of forum guidelines.

    here is the program scenario:

    Suppose you invest $100.00 at an interest rate of 10% per year. The interest is computed at the end of each year, and is added to your account (i.e., the interest is compounded yearly). Print what the account value will be each year for 20 years. Use data type double. Do not use the pow function.

    This is what I have so far:
    Code:
    /* 20 yr interest */
    #include <stdio.h>
    
    /* function main begins program execution */
    int main( void )
    {
       double amount; 
       double amount2;
       double principal = 100.00;
       int year;
       double rate = .10;   
      
       printf("Year\tAmount on deposit ($)\n");
    
       for(year=1; year<=20; year++)
       {   
          amount = principal * rate;
          amount2 = (amount + principal) * rate;
           
       printf("%4d\t\t%8.2lf\n", year, amount2);
       }
    
       return 0; /* indicate that program ended successfully */
    
    } /* end function main */

  2. #2
    Registered User
    Join Date
    Dec 2010
    Posts
    113
    Code:
    /* 20 yr interest */
    #include <stdio.h>
    
    /* function main begins program execution */
    int main( void )
    {
       double amount; 
       double amount2;
       double principal = 100.00;
       int year;
       double rate = .10;   
      
       printf("Year\tAmount on deposit ($)\n");
       amount=principal;
    
       for(year=1; year<=20; year++)
       {   
          amount = amount * rate;
          amount2 = (amount + principal) * rate;
          amount=amount2;
           
       printf("%4d\t\t%8.2lf\n", year, amount2);
       }
    
       return 0; /* indicate that program ended successfully */
    
    } /* end function main */
    Have a look at this one.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    It would be beneficial if you worked out - on paper - how you would compute the interest and (new) principal for each year BEFORE writing code.

    I'll give two hints in the form of questions: why are you multiplying amount2 by rate? And why are you not updating principal each time through the loop?
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Dec 2010
    Posts
    113
    Quote Originally Posted by grumpy View Post
    why are you multiplying amount2 by rate?
    My correction misses that one too.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Try ...

    Code:
    for (int x = 0; x < years; x++ )
      { Principle += (Principle * Rate);
         printf(" Year %d = %.2f \n", x + 1, Principle); }

  6. #6
    Registered User
    Join Date
    Feb 2011
    Location
    San Diego
    Posts
    10
    awesome i finally got it. thanks for all the help. i think i'm getting burnt out. anyway, i used:

    Code:
       double principal = 100.00;
       int year;
       double rate = .10;   
      
       printf("Year\tAmount on deposit ($)\n");
    
       for ( year = 1; year <= 20; year++ )
      { principal += (principal * rate);
         printf(" %d\t\t%.2f \n", year, principal); }

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    So ... ... be sure to let me know how I did on your homework...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-11-2009, 12:54 PM
  2. need help with compound interest
    By JoelearningC in forum C Programming
    Replies: 4
    Last Post: 03-09-2008, 10:32 AM
  3. Compound Interest Formula
    By veronicak5678 in forum C++ Programming
    Replies: 3
    Last Post: 09-16-2007, 05:16 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. compound interest
    By bliznags in forum C Programming
    Replies: 11
    Last Post: 03-20-2005, 01:46 AM