Compound Interest?

This is a discussion on Compound Interest? within the C Programming forums, part of the General Programming Boards category; I have been trying for many hours to get this to work, but alas no success. i have about 6 ...

  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
    93
    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,429
    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%.

  4. #4
    Registered User
    Join Date
    Dec 2010
    Posts
    93
    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, 11:54 AM
  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, 12:46 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21