Thread: Compound Interest Formula

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    36

    Compound Interest Formula

    I can't seem to figure this out, though it should be pretty simple. Here are my instructions:
    Compound interest

    Compound.cpp

    Write a program that calculates compound interest. The program should ask the user for the starting dollar amount and the daily increase (as a percentage), and the number of days. A loop should then be used to display the day, the amount of interest earned on that day and the account balance on that day. The program should also display the total interest earned. Output should be as follow



    Initial amount in dollars? 100
    Interest rate in percentage? 10
    Number of days? 3




    Day Earned interest Balance
    -----------------------------------------------
    1 $10 $110.00
    2 $11 $121.00
    3 $12.10 $133.10

    Total Interest earned: $33.10


    Validation:
    Dollar amount should be between 10 and 10000
    Interest rate should be between 1 and 22
    Number of days should be between 2 and 30

    The output should be formatted and aligned according to the above.


    Here is my code:
    Code:
    #include <cstdio>
    #include <math.h>  
    #include <iostream>
    
    using namespace std;
    
    int main ()
    {
    double dollars, amount, interest; 
    int days, rate, decimalrate, a=1;
    bool notValid;
    
       
       do{
       notValid=false;                                       //Validation for amount
       cout << "\nPlease enter dollar starting amount?" ; 
       cin >> dollars;
       if (dollars <10 || dollars >10000)
           {
            cout << "\nYou have entered invalid data"; 
            notValid=true;
            }
       } while (notValid);
    
       do{
       notValid=false;                                       //Validation for rate
       cout << "\nWhat is the daily increase (as a percentage)?" ;
       cin >> rate;
       if (rate <1 || rate >22)
           {
            cout << "\nYou have entered invalid data"; 
            notValid=true;
            }
       } while (notValid);
      
       do{
       notValid=false;                                       //Validation for days
       cout << "\nEnter the number of days:" ;
       cin >> days;
       if (days<2 || days >30)
           {
            cout << "\nYou have entered invalid data"; 
            notValid=true;
            }
       } while (notValid);
      
       {
           cout << "\n\nDay     Earned Interest     Balance"; 
           cout << "\n-----------------------------------\n";
            
           
            for(a==1; a <= days; )                           //Loop for display 
                    { 
                      decimalrate=rate/100;
                      amount=dollars +(dollars*decimalrate);
                      interest=amount-dollars;                     
                      cout << a;
                      cout << "           ";  
                      printf ("%10.2lf", interest); 
                      cout << "               "; 
                      printf ("%10.2lf", amount);
                      cout << "\n";                   
                      a++; 
                      amount=amount+interest;                                  //Increments a
                      }      
                system("pause");
                
    
        }
    }
    Why is my interest 0?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quick guess:
    Code:
    decimalrate=rate/100;
    Try
    Code:
    decimalrate=rate/100.0;
    And change decimalrate to a double?
    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
    Sep 2007
    Posts
    36
    Thanks! That helped get the first calculation. But now the output for the "earned interest" is $10 for everyday, making the balance $110 everyday. Why is the interest not accumulating?

  4. #4
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Because when calculating interest you havent added any code that takes into account the number of days that have passed.

    [edit] IE: interest+=(amount-dollars);[/edit]
    Last edited by mike_g; 09-16-2007 at 05:20 PM.

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, 12:54 PM
  3. need help with compound interest
    By JoelearningC in forum C Programming
    Replies: 4
    Last Post: 03-09-2008, 10:32 AM
  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