Sams Teach Yourself C in 21 Days

(singke) #1
LISTINGT&R 6 mortgage.c. The mortgage calculator
1: /* Calculates loan/mortgage payments. */
2:
3: #include <stdio.h>
4: #include <math.h>
5: #include <stdlib.h>
6:
7: int main( void )
8: {
9: float principal, rate, payment;
10: int term;
11: char ch;
12:
13: while (1)
14: {
15: /* Get loan data */
16: puts(“\nEnter the loan amount: “);
17: scanf(“%f”, &principal);
18: puts(“\nEnter the annual interest rate: “);
19: scanf(“%f”, &rate);
20: /* Adjust for percent. */
21: rate /= 100;
22: /* Adjust for monthly interest rate. */
23: rate /= 12;
24:
25: puts(“\nEnter the loan duration in months: “);
26: scanf(“%d”, &term);
27: payment = (principal * rate) / (1 - pow((1 + rate), -term));
28: printf(“Your monthly payment will be $%.2f.\n”, payment);
29:
30: puts(“Do another (y or n)?”);
31: do
32: {
33: ch = getchar();
34: } while (ch != ‘n’ && ch != ‘y’);
35:
36: if (ch == ‘n’)
37: break;
38: }
39: return 0;
40: }

This program assumes a standard loan, such as a typical fixed-rate car or home
loan. The payment is calculated using the following standard financial formula:
payment= (P* R) / ( 1 - (1 + R)^(-T))

562 Type & Run 6

INPUT

ANALYSIS

31 448201x-T&R6 8/13/02 11:19 AM Page 562

Free download pdf