/* loan.c by Nannette Thacker Loan Program: User inserts borrowed amount, annual interest rate, monthly payment amount. Returns monthly payment of interest, how much toward principal, balance owned on principal at end of month. gcc loan.c -o loan.exe */ #include #include /* prototypes */ void getvalues(float *, float *, float *); // function to get the values from the user void calculate(int , float *, float *, float *, float , float ); // calculate void printhead(float, float, float); void printsubhead(void); int main() { int month ; float interest ; float principal ; float balance ; // amount borrowed, entered by user float rate ; // interest rate float payment ; // monthly payment int counter ; // for number of lines to screen int firstime ; // is this first time through loop? getvalues(&balance, &rate, &payment); // get the values from the user printhead(balance,rate, payment); if(rate > 100) // check if user input an interest rate greater than 100 { printf("That's highway robbery! I refuse to continue!\n\n"); exit(0); } else if(rate < 1 || payment < 1) { printf("That is not a valid loan. Transaction aborted.\n\n"); exit(0); } rate = rate / 1200 ; // get monthly interest rate counter = 0 ; month = 1 ; // initialize the counter while(balance > 0) { if (counter == 0 ) printsubhead(); // print subhead once for each screen full calculate(month, &interest,&principal,&balance,rate,payment); month ++ ; counter ++ ; if(counter >= 12) { counter = 0 ; printf("\nPlease press any key to continue...\n"); getchar(); printf("\n\n\n\n\n\n\n\n"); } else if(balance == 0) printf("\n\nThaaaaaaaaaaaaaaaaaaat's aaaaaaaallllllll folks!!\n\n\n\n"); } return(0); } void printhead(float bbalance, float rrate, float ppayment) { printf("\n\n TABLE OF MONTHLY PAYMENTS\n\n"); printf("Original Principal: %.2f Interest Rate %.2f percent\n\n",bbalance, rrate); printf(" Monthly Payment Amount: %.2f\n\n",ppayment); return ; } void printsubhead() { printf("MONTH INTEREST PRINCIPAL PAYMENT BALANCE\n"); return ; } void calculate(int mmonth, float * iinterest, float * pprincipal, float * bbalance, float rrate, float ppayment) { // interest each month = loan balance at start of month * monthly interest rate // principal payment each month = monthly payment amount - interest for that month // balance each month = balance last month - principal payment this month *iinterest = *bbalance * rrate ; *pprincipal = ppayment - *iinterest ; *bbalance = *bbalance - *pprincipal ; if(*bbalance <= 0) // on final payment, don't allow balance to be less than 0 { *pprincipal = *pprincipal + *bbalance ; *bbalance = 0 ; printf("FINAL PAYMENT OF %.2f:\n",*pprincipal + *iinterest); } printf("%4d\t%6.2f\t %10.2f\t %10.2f\n",mmonth,*iinterest,*pprincipal,*bbalance); // show them the first line and the error of their ways...then if(*pprincipal < 0) // check for error in case the monthly payment is so low that nothing would go to principal { printf("\n\nYou're monthly payment is TOO LOW for this INTEREST RATE! Can't continue!\n\n"); exit(0); } return ; } void getvalues(float * bbalance, float * rrate, float * ppayment) { printf("\n\nPlease input the amount borrowed: "); scanf("%f",bbalance); printf("\nPlease input the annual interest rate: "); scanf("%f",rrate); printf("\nPlease input the the monthly payment amount: "); scanf("%f",ppayment); printf("\n\n"); getchar(); }