# compound interest

Printable View

• 03-19-2005
bliznags
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
• 03-19-2005
Dave_Sinkula
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;```
• 03-19-2005
bliznags
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
• 03-19-2005
Dave_Sinkula
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.
• 03-19-2005
bliznags
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
• 03-19-2005
Dave_Sinkula
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.

Oh, I forgot I changed this line, too.
Code:

```  balance = principal;   for ( months = 0; months < total_months; ++months )```
• 03-19-2005
bliznags
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.
• 03-19-2005
Dave_Sinkula
Try removing this line after the for loop.
Code:

`balance = interest + principal;`
The loop calculates balance -- don't discard all that work.

Use %.2f instead of %0.2lf for doubles in printf.
• 03-19-2005
bliznags
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.
• 03-19-2005
anonytmouse
There is also a formula to calculate compound interest.

For Thantos :cool: [/edit]
• 03-19-2005
Thantos
Bah that page didn't even mention the pert formula
• 03-20-2005
Salem
Only 3 minutes between original posts.

Further replies here please
http://cboard.cprogramming.com/showthread.php?t=63232