my program needs to print out the following results:
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
it is all fine in my code up until it calculates to print this line:
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
instead i am getting:
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
here is my 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;
}
the math in my loop has to be what is doing it, but i dont see why....
can anyone clear my problem up for me please?
thank you