compound interest

This is a discussion on compound interest within the C Programming forums, part of the General Programming Boards category; i have to make a program to print out the following: Enter a principal amount: 1000 Enter an annual interest ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    9

    compound interest

    i have to make a program to print out the following:

    Enter a principal amount:
    1000
    Enter an annual interest rate:
    7
    Your first month of interest will be: $5.83
    Enter the number of years your money will be in the bank:
    30
    You plan to deposit $1000.00 for a term of 30 years.
    The total amount of interest you will earn will be $7116.50
    Your final balance will be $8116.50.

    i have tried numerous things to solve this but everything i do is wrong. my while loop keeps returning extremely large numbers. my code looks like this.

    Code:
    #include <stdio.h>
    int main()
    {
      double principal, interest_rate, balance, interest;
      int months, total_months, total_years;
    
      total_months = 12 * total_years;
      months = 1;
      balance = interest + principal;
     
      printf("Enter a principal amount:\n");
      scanf("%lf", &principal);
    
      printf("Enter an annual interest rate:\n");
      scanf("%lf", &interest_rate);
    
      printf("Your first month of interest will be: $%0.2lf\n", interest_rate / 12 * (principal / 100));
    
      printf("Enter the number of years your money will be in the bank:\n");
      scanf("%d", &total_years);
    
      printf("You plan to deposit $%0.2lf for a total of %d years.\n", principal, total_years);
    
      while(months <= total_months)
        {
          interest = principal + (interest_rate / 12 * (principal / 100));
          interest += principal;
          months++;
        }
    
      printf("The total amount of interest you will earn will be %0.2lf\n", interest - principal);
    
      printf("Your final account balance will be %0.2lf\n", balance);
    
      return 0;
    
    }
    any help will be greatly appreciated

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    First, obtain the data before performing calculations on it.
    Code:
      months = 1;
    
      printf("Enter a principal amount:\n");
      scanf("%lf", &principal);
    
      printf("Enter an annual interest rate:\n");
      scanf("%lf", &interest_rate);
    
      printf("Your first month of interest will be: $%0.2lf\n", interest_rate / 12 * (principal / 100));
    
      printf("Enter the number of years your money will be in the bank:\n");
      scanf("%d", &total_years);
    
      printf("You plan to deposit $%0.2lf for a total of %d years.\n", principal, total_years);
    
      total_months = 12 * total_years;
      balance = interest + principal;
    Last edited by Dave_Sinkula; 03-19-2005 at 06:20 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    9
    thanks a lot, that helped make the program print out

    Enter a principal amount:
    1000
    Enter an annual interest rate:
    7
    Your first month of interest will be: $5.83
    Enter the number of years your money will be in the bank:
    30
    You plan to deposit $1000.00 for a total of 30 years.
    The total amount of interest you will earn will be 1005.83
    Your final account balance will be 3005.83

    but my math is wrong in the code, but it seems like it should work.
    i'm supposed to get a total interest of 7116.50 and a final balance of 8116.50. any word on fixing this problem?

    thanks again or clearin that up for me though

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Second, I tried to remove a repeated calculation.
    Code:
       printf("Enter an annual interest rate:\n");
       scanf("%lf", &interest_rate);
       
       interest_rate /= 12;
       interest_rate /= 100;
       printf("Your first month of interest will be: $%.2f\n", principal * interest_rate);
    Third, I prefer for loops, but I think you calculate interest something like this.
    Code:
       for ( months = 0; months < total_months; ++months )
       {
          interest  = balance * interest_rate;
          balance  += interest;
       }
    
       printf("The total amount of interest you will earn will be %.2f\n", balance - principal);
       printf("Your final account balance will be %.2f\n", balance);
    Note also the format specifiers for printf.

    Last, throwing a couple of printfs into the loop while you are developing code is a good way to learn how to debug your own code.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    9
    i must ask you how do i put printfs in the loop to test it out?

    also when i ran the program it came out with the following outcome...

    Enter a principal amount:
    1000
    Enter an annual interest rate:
    7
    Your first month of interest will be: $5.83
    Enter the number of years your money will be in the bank:
    30
    You plan to deposit $1000.00 for a total of 30 years.
    The total amount of interest you will earn will be 0.00
    Your final account balance will be 1000.00

    it doesnt impliment the total interest.
    i'm sorry for all this, i'm very new to programming and am often very confused/lost.
    i guess it is calulating the interest to 0, and i can't understand why. it looks like it should be doing the math given the loop.

    about the format specifiers.... i thought i had to use %lf when they are declared as type double.

    thanks again

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by bliznags
    i must ask you how do i put printfs in the loop to test it out?
    Just how it sounds...
    Code:
       for ( months = 0; months < total_months; ++months )
       {
          interest  = balance * interest_rate;
          balance  += interest;
          printf("months = %2d, interest = %.2f, balance = %.2f\n", months, interest, balance);
       }
    Post the code of your latest attempt.

    [edit]Oh, I forgot I changed this line, too.
    Code:
     balance = principal;
    
       for ( months = 0; months < total_months; ++months )
    Last edited by Dave_Sinkula; 03-19-2005 at 07:01 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Mar 2005
    Posts
    9
    hey, it is still coming up witht he wrong data at the end, heres the code:
    Code:
    #include <stdio.h>
    int main()
    {
      double principal, interest_rate, balance, interest;
      int months, total_months, total_years;
    
      printf("Enter a principal amount:\n");
      scanf("%lf", &principal);
    
      printf("Enter an annual interest rate:\n");
      scanf("%lf", &interest_rate);
    
      interest_rate /= 12;
      interest_rate /= 100;
    
      printf("Your first month of interest will be: $%0.2lf\n", principal * interest_rate);
    
      printf("Enter the number of years your money will be in the bank:\n");
      scanf("%d", &total_years);
    
      printf("You plan to deposit $%0.2lf for a total of %d years.\n", principal, total_years);
    
      total_months = 12 * total_years;
      balance = principal;
    
      for ( months = 0; months < total_months; ++months )
        {
          interest  = balance * interest_rate;
          balance  += interest;
        }
    
      balance = interest + principal;
    
      printf("The total amount of interest you will earn will be %.2lf\n", balance - principal);
    
      printf("Your final account balance will be %.2lf\n", balance);
    
      return 0;
    
    }
    thanks a lot for all the help.
    Last edited by bliznags; 03-19-2005 at 07:13 PM. Reason: missed somehitng

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Try removing this line after the for loop.
    Code:
    balance = interest + principal;
    The loop calculates balance -- don't discard all that work.

    [edit]Use %.2f instead of %0.2lf for doubles in printf.
    Last edited by Dave_Sinkula; 03-19-2005 at 07:40 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    Registered User
    Join Date
    Mar 2005
    Posts
    9
    awesome man, that did it, it works perfectly now. thank you so much for the assistance....

    if it isnt so much to ask do you think you could take a gander at my other post on the forum to see if you know a solution to it. its somewhat similar to this one, but different in the end... the link is,

    http://cboard.cprogramming.com/showt...748#post449748

    thanks a lot.

  10. #10
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    There is also a formula to calculate compound interest.

    [edit]For Thantos [/edit]
    Last edited by anonytmouse; 03-19-2005 at 10:27 PM.

  11. #11
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Bah that page didn't even mention the pert formula

  12. #12
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    Only 3 minutes between original posts.

    Further replies here please
    while loop help
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

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

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