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