Compound interest in C How can I use the formula A P 1 rnn
Compound interest in C
How can I use the formula A = P (1 + r/n)^nt in C?
I\'ve been trying to use pow inside a for loop to calculate the daily compound interest (2%):
int years, initialYears;
 double initialAmount, dailyAmount;
 double pow(double x, double y);
printf(\"Enter initial amount: \");
 scanf(\"%lf\", &initialAmount);
 printf(\"\ \");
 printf(\"Enter number of years: \");
 scanf(\"%d\", &years);
 printf(\"\ \");
printf(\"\\tYear Daily \ \");
 printf(\"\\t---- ----- \ \");
for (initialYears = 0; initialYears <= years; initialYears++)
 {
 dailyAmount = initialAmount * pow((1 + (.02/365), (365 * initialYears)));
 printf(\"\\t%d %lf\ \", initialYears, dailyAmount);
}
Instead of getting the right number, I get a different one. When I try something like this for the loop, I get a very close number for the daily amount but it\'s missing the decimal values (for example if initialAmount = 1000, then for the 1st year I should get 1020.200781 but I only get 1020.000000).
dailyAmount = initialAmount * (1 + (.02/365) * (365 * initialYears));
 printf(\"\\t%d %lf\ \", initialYears, dailyAmount);
Solution
we need to modify the printf statement which we are using to show the dailyAmount.
use printf(\"\\t%d %.8lf\ \", initialYears, dailyAmount);
we have use %.8lf to show the dailyAmount , it will show the decimal places till 8.
If we need to show less we can decrease the same.

