The UNIX Forums
"Join the Network of UNIX System Users"


 
Subject: bad mortgage calculation code
ddt449
Newbie
Rank: 1



UID 20
Digest Posts 0
Credits 0
Posts 38
Reading Access 10
Registered Apr 25, 2007
Status Offline
Post at Jun 15, 2007 08:09 AM  Profile | P.M. 
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.


Top
 

 

All times are GMT, the time now is Jul 31, 2010 03:33 AM

Powered by Discuz! 5.0.0  © 2001-2006 UNIX Forums
Processed in 0.004628 second(s), 8 queries

Clear Cookies - Contact Us - UNIX Help - Archiver - WAP