Thread: Compound Interest Formula

1. 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. Quick guess:
Code:
decimalrate=rate/100;
Try
Code:
decimalrate=rate/100.0;
And change decimalrate to a double?

3. 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. Because when calculating interest you havent added any code that takes into account the number of days that have passed.

 IE: interest+=(amount-dollars);[/edit]

Popular pages Recent additions