• 03-21-2005
bliznags
ive been asked the following question.

Start with an initial principal amount and a fixed payment amount. Every month, the payment amount is added to the principal, along with th interest that the principal has earned for the past month, so the principal grows in two ways. Show this output:

enter an initial depost:
2000
Enter an annual interest rate
7
your first month of interest will be 11.67
enter the number of years your annuity will be in the bank:
30
enter the amount of your monthly payment
200
you started with a principal of 2000.00
your payment schedule consists of 360 payments of 200.00
the total amount of your payments will be 72000.00
the total amount of interest you will earn will be 186227
at the end of 30 years your annuity will be 260227.19

here is what im getting though:

enter an initial depost:
2000
Enter an annual interest rate
7
your first month of interest will be 11.67
enter the number of years your annuity will be in the bank:
30
enter the amount of your monthly payment
200
you started with a principal of 2000.00
your payment schedule consists of 360 payments of 200.00
The total amount of your payments will be \$-200.00.
The total amount of interest you earn will be \$4200.00.
At the end of 1085227007 years your annuity will be worth \$-NaN.

My code is as follows:
Code:

```#include <stdio.h> int main() {   double initial_principal, annual_interest_rate, monthly_payment, total, interest = 0,     total_payments, annuity;     int years_annuity, months = 0, payments = 0;   total = initial_principal;   printf("Enter an initial deposit amount:\n\n");   scanf("%lf", &initial_principal);   printf("Enter an annual interest rate:\n");   scanf("%lf", &annual_interest_rate);     printf("Your first month of interest will be: %.2lf\n", annual_interest_rate / 12 * (initial_principal / 100));   printf("Enter the number of years your annuity will be in the bank:\n");   scanf("%d", &years_annuity);   printf("Enter the amount of your monthly payment:\n");   scanf("%lf", &monthly_payment);   printf("You started with a principal of \$%0.2lf\n", initial_principal);   while ( months < years_annuity * 12 )   {     interest = interest + (annual_interest_rate / 12 * (initial_principal / 100));     printf("%.2lf\n", interest);       total_payments = (total * (1 + (12 / annual_interest_rate * .01))) - monthly_payment;     printf("%.2lf\n", total_payments);       ++months;   }     annuity = interest + total_payments + total;   printf("Your payment schedule consists of %d payments of \$%.2lf,\n", months, monthly_payment);   printf("The total amount of your payments will be \$%.2lf.\n", total_payments);   printf("The total amount of interest you earn will be \$%.2lf.\n", interest);   printf("At the end of %d years your annuity will be worth \$%.2lf.\n", annuity);   return 0; }```
my problem lies inside the loop with my math calculations. I've been working on this forever but cant figure out the right equations to get it to print out the desire results. if anyone knows what could be causing this please enlighten me because i can't figure it out.

Thanks
• 03-21-2005
InvariantLoop
Code:

`printf("Your payment schedule consists of %d payments of \$%.2lf,\n", months, monthly_payment);`
and
Code:

`printf("At the end of %d years your annuity will be worth \$%.2lf.\n", annuity);`
you see the difference between them? what does %d give you on the satemetn above? Also NAN means Not a Number so one of your calculations is wrong.
• 03-21-2005
spydoor
you get NaN becuase your last printf statment expects two variables and you only pass one.

to keep track of your total payments you simply need

Code:

`total_payments = total_payments + monthly_payment;`

you need to keep track of your annuity as you go, and apply the interest_rate to your current annuity

annuity should start as your initial_principal.

your interest for each month is
Code:

`interest = interest_rate * annuity`
Code:

`annuity = annuity + interest + monthly_payment`
you also need another variable for total_interest.. this isn't used in a calculation but just to display later.

one more thing

when you do this
Code:

`annual_interest_rate / 12 * (initial_principal / 100))`
you should proably actually be doing
Code:

`annual_interest_rate / 12 /100 * (initial_principal ))`
it works out the same, but makes more sense to convert your interest rate to a decimal