# Compound Interest?

• 02-25-2011
jbone0881
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 */

• 02-25-2011
GokhanK
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.
• 02-25-2011
grumpy
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?
• 02-25-2011
GokhanK
Quote:

Originally Posted by grumpy
why are you multiplying amount2 by rate?

My correction misses that one too.
• 02-25-2011
CommonTater
Try ...

Code:

for (int x = 0; x < years; x++ )
{ Principle += (Principle * Rate);
printf(" Year %d = %.2f \n", x + 1, Principle); }

• 02-25-2011
jbone0881
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); }

• 02-25-2011
CommonTater
So ... :D ... be sure to let me know how I did on your homework...