bad mortgage calculation code
bad mortgage calculation code
hello,
i'm teaching myself c and attempting to write a little program that will calculate the monthly payments on a fixed-rate mortage:
/* aaron's mortgage foo */
// mortgage.c
#include <stdio.h>
#include <math.h>
int main(void)
{
float interestrate; // the bank's fixed interest rate
float interest; // monthly interest rate
int mortgage; // cost of the house
int term; // how many years is the mortgage
int totmonths; // total number of payments
float monthlypayment; // your monthly mortgage payment
interest = interestrate / 1200;
totmonths = term / 12;
printf("how much is the mortgage? ");
scanf(" %d", &mortgage);
printf("for how many years? ");
scanf(" %d", &term);
printf("what is the bank's interest rate? ");
scanf(" %f", &interestrate);
monthlypayment = mortgage * pow(1 + interest, totmonths) * interest / pow(1 + interest, totmonths) - 1;
printf("your monthly payments are %.2f\n.", monthlypayment);
return 0;
}
when i fat-finger the numbers into a calculator i get correct answers so i think monthlypayment formula is accurate; however, program outputs monthly payments of -1.00 no matter what numbers i input. obviously, i'm overlooking something. any help? thanks.
|