# Extra Credit Homework: Auto Loans

## Objectives

This assignment will review several programming features including if statements, switch statements, for loops and functions.

## Instructions

This extra credit assignment has 4 parts. Each part is worth 1 percentage point of your final grade. You can do as many parts as you like, but you must do them in order. For example, you can do Parts 1 and 2 and skip Parts 3 and 4, but you cannot just do Parts 1 and 4.

As has been the policy in this class, extra credit points will be awarded only for submissions that complete most of the assignment. Work that would be marked as "minimal submission" will receive 0 extra credit points.

Remember to properly indent and to comment your programs.

## Assignment

Your assignment is to write a program that helps the user figure out monthly payments on automobile loans. For Parts 1, 2 and 3, you can check your program against the auto loan calculator on Bankrate.com.

Our assumption throughout this assignment is that the user is getting a traditional fixed rate automobile loan. Typically, the buyer borrows some amount of money from a financial institution. This amount is called the "principal". The buyer pays interest on the loan, usually quoted as an annual percentage rate. The monthly interest rate is defined as 1/12 × the annual percentage rate. (This is totally wrong mathematically, but that's another story.) The term of the loan is the number of months that the buyer has to repay the loan.

The buyer makes fixed monthly payments until the loan is paid off. This monthly payment can be computed using the formula:

payment = ( i × P ) / [ 1 − ( 1 + i)−n ]

where i is the monthly interest (in decimal, not as a percentage), P is the principal and n is the number of months.

For example, if the buyer borrows \$20,000 for 48 months on a 4.5% loan (annual percentage rate),

i = 0.045 / 12 = 0.00375
P = 20000
n = 48

Then, we have:

i × P = 0.00375 × 20000 = 75
( 1 + i)−n = (1 + 0.00375)−48 = 0.835551...

So, the monthly payment is

payment = 75 / ( 1 − 0.835551) = 75 / 0.164449 = 456.07

after rounding to the nearest cent. You can confirm these calculations with the auto loan calculator on Bankrate.com.

## Part 1: payment function

For Part 1, your assignment is to write a function that computes the monthly payment given the principal, interest rate and the term of the loan using the formula explained above. The principal, interest rate and term of the loan should be given as arguments to the function. Except for debugging, there should not be any printf() statements in your function.

To test your function, write a main() function that prompts the user for the principal, interest rate and term of the loan. A sample run of your program might look like:

```
Enter loan amount: 20000
Annual Interest rate (enter 5.25 for 5.25%): 4.5
Number of payments (enter 36 for 36 months): 48

```

Record several runs of your program using the script command. Use Bankrate.com to check your answers. Then, submit your C program and sample runs:

```submit cs104_chang extra formula.c
submit cs104_chang extra typescript1
```

## Part 2: amortization table

For Part 2, your assignment is to print out an amortization table for an automobile loan. Go to the auto loan calculator on Bankrate.com. Enter \$20000, 4 years and 4.5% for the amount, term and interest rate and click on the "Show/Recalculate Amortization Table" button. You get a table with 48 rows. Each row shows the monthly balance of the loan.

For your program, the first few rows of the table might look like:

```       Interest   Principal
Month    paid       paid     Balance
1      75.00     381.07    19618.93
2      73.57     382.50    19236.43
3      72.14     383.93    18852.50
...
```

These figures are computed as follows. Recall from the example above that the monthly interest rate on a 4.5% loan is 0.00375. So, the interest on the original \$20,000 for the first month is:

20000 × 0.00375 = 75

Thus, of the \$456.07 monthly payment, \$75 goes towards interest and the remaining \$456.07 - 75 = \$381.07 goes towards paying down the principal. The balance on the loan at the end of the first month is:

\$20000 − 381.07 = \$19618.93

Similarly, interest at the end of the second month is \$19618.93 × 0.00375 = \$73.57, which leaves \$456.07 − 73.57 = \$382.50 to pay down the principal. Thus, the balance becomes \$19618.93 − 382.50 = \$19236.43.

Your program should print out the rest of the amortization table for the remaining term of the loan. You can check your answer on Bankrate.com.

Note: when you need to calculate the monthly payment in this part of the assignment, you must call your payment function from Part 1.

Record several runs of your program using the script command and submit your program as table.c to extra.

```submit cs104_chang extra table.c
submit cs104_chang extra typescript2
```

## Part 3: rebate versus lower rate

When new cars go on sale, buyers are often offered a choice between a rebate (which reduces the price of the car) or a lower interest rate. For this part of the assignment, write a main program that the helps the user determine whether a rebate or a lower interest rate would result in lower monthly payments.

A sample run of your program might look like:

```Enter loan amount: 20000
Annual Interest rate (enter 5.25 for 5.25%): 4.5
Number of payments (enter 36 for 36 months): 48
Rebate amount: 1000
Lower interest in lieu of rebate: 1.3

Your monthly payment with the rebate: 433.27

Your monthly payment with the lower rate: 427.82

Taking the lower interest rate will give you a lower monthly payment

```

As in Part 2, you must call your monthly payment function from Part 1 when you need to compute the monthly payment. You can check your answer using the Bankrate.com auto loan calculator.

Using the script command, record several runs of your program (with different rebates and interest rates). Then submit your program as compare.c to extra.

```submit cs104_chang extra compare.c
submit cs104_chang extra typescript3
```

## Part 4: menu driven program

For the final part of this assignment, you must package up the first 3 parts into a menu driven program. Your program must present 4 choices to the user: calculate monthly payment, display amortization table, compare rebate vs lower rate and quit. After the user selects a choice, the program should return to the main menu again. (This repeats until the user chooses to quit the program.)

You should take a look at nobreak2.c to see how such a menu can be constructed from a switch statement inside a while loop. However, you should not stuff too many lines of code in the cases of your switch statement. (The switch statement would be too difficult to read.) Instead, you should call a separate function to perform the selected task. (Hint: rename the main() functions in your programs for Parts 1, 2 and 3.)

When you are done, record several runs of your program using the script command and submit your program as autoloan.c to extra.

```submit cs104_chang extra autoloan.c
submit cs104_chang extra typescript4
```