Project1 - Amortization Schedule |
|
Next > |
Background
A loan has the following parameters: an initial balance, an interest rate and a length to pay the loan back. From these parameters, we can calculate a monthly payment.
You're thinking about taking out a loan (for a car, school or a home) and you want to know how much of each payment is going to the balance.
You decided to write code to calculate to calculate an
amortization schedule for specified loan terms.
The formula for calculating a monthly payment is:
J
M = P * (J + --------------)
( 1 + J)N*T - 1
or
M = P * (J + (J / ((1+J)N*T - 1)))
where:
P is the initial amount of the loan
R is the annual interest rate
N is the number of compounding periods in a year (e.g., 12 for monthly)
J is the monthly interest rate (R / N)
T is the number of years
To calculate the amortization schedule, you need to calculate the each month's interest, principal and new balance.
To calculate the interest for a given month, multiply the monthly interest rate by the current balance (J * balance).
Whatever is above and beyond the interest goes to paying down the balance. This is called the principal.
To calculate the principal for a given month, subtract the monthly interest from the monthly payment.
Use this amount to update the balance for that month.
Note, financial software often performs calculations using cents instead of dollars.
For this assignment, round the monthly payment to the nearest cent and primarily use
ints instead of
floats or
doubles. Note, you should not need to round the other amounts.
Assignment
For this project, you will write a C program to calculate an amortization schedule based on loan terms specified in a file.
The format of the file is as follows:
<loan amount (in cents)>\t<annual interest rate (a percentage, not a rate)>\t<number of years (assume that this will always be a whole number)>
Note: Where
'\t' is a tab character.
Additionally, only display the amortization schedule for the first and last six months.
Examples
Let's assume that you're trying to buy a $10,000 car and you're looking at a 5 year loan at 6% annual interest.
The
input file would look like:
1000000 6.0 5
So,
P = $10,000.00
R = 0.06 (notice, it's a rate, not a percentage)
J = (0.06) / 12 = 0.005
N = 12 payments / year
T = 5 years
The monthly payment is then,
M = $10,000 * (0.005 + (0.005 / ((1+0.005)(5*12) - 1))) = $193.33 (rounded to the nearest cent)
The amount of interest charged each month changes and is calculated as
J times the current balance. The principal amount also changes each month because it is the monthly payment amount minus the interest for that month. The balance for that month is then updated to be the previous month's balance minus the amount of principal paid in the current month.
So, for our auto loan, the interest for the first month is
J * $10,000.00 = $50.00.
Therefore, the principal is $193.33 - $50.00 = $143.33 and giving us a new balance of $10,000.00 - $143.33 = $9,856.67.
The interest for the second month is
J * $9,856.67 = $49.28, the principal is then $193.33 - $49.287 = $144.04 and giving us a new balance of $9,856.67 - $144.04 = $9,712.63.
Given the input file above, your program should match the following
output exactly:
Please enter the loan terms filename: Processing terms from project1-inputA.tab
Processing 5-year loan for 1000000 cents at an annual interest rate of 0.060
Amortization Schedule
Initial Balance (cents): 1000000
APR: 0.060
Years: 5
Monthly Payment (cents): 19333
Month Int. Princ. Balance
1 5000 14333 985667
2 4928 14405 971262
3 4856 14477 956785
4 4783 14550 942235
5 4711 14622 927613
6 4638 14695 912918
...
55 569 18764 95184
56 475 18858 76326
57 381 18952 57374
58 286 19047 38327
59 191 19142 19185
60 95 19238 -53
Note: There are tab characters (
'\t') in-between the values in the table to attempt to make it line-up vertically. Additionally, the ellipsis, "
...", is required.
Let's calculate another the monthly loan payment, this time for a
Pagani Zonda Revolucion.
For that car, let's use:
P = $2,800,000.00
R = 0.084%
J = (0.084) / 12 = 0.007
N = 12 payments / year
T = 2 years
M = $2,800,000 * (0.007 + (0.007 / ((1+0.007)(2*12) - 1))) = $127,147.84
That's the MONTHLY payment each month for 2 years!
So, the interest for the first month is
J * $2,800,000.00 = $19,600.00.
Therefore, the principal is $127,147.84 - $19,600.00 = $107,547.84 and giving us a new balance of $2,800,000.00 - $107,547.84 = $2,692,452.16.
The interest for the second month is
J * $2,692,452.16 = $18,847.17, the principal is $127,147.84 - $18,847.17 = $108,300.68 and giving us a new balance of $2,692,452.16 - $108,300.68 = $2,584,151.48.
Given the
input file for these loan terms, your program should match the following
output exactly:
Please enter the loan terms filename: Processing terms from project1-inputB.tab
Processing 2-year loan for 280000000 cents at an annual interest rate of 0.084
Amortization Schedule
Initial Balance (cents): 280000000
APR: 0.084
Years: 2
Monthly Payment (cents): 12714784
Month Int. Princ. Balance
1 1960000 10754784 269245216
2 1884716 10830068 258415148
3 1808906 10905878 247509270
4 1732564 10982220 236527050
5 1655689 11059095 225467955
6 1578275 11136509 214331446
...
19 521177 12193607 62260368
20 435822 12278962 49981406
21 349869 12364915 37616491
22 263315 12451469 25165022
23 176155 12538629 12626393
24 88384 12626400 -7
Alternatively, if the file can not be opened, then display an error message. For example, if the user input
does_not_exist.tab then display the following:
Please enter the loan terms filename: ERROR: Unable to open does_not_exist.tab!
Submission
Before submitting this assignment, make sure there are no compilation errors. If there are, fix them before submitting.
You may need to edit and compile your program multiple times before it compiles successfully.
Verify that the output is correct (including dollar signs, tabs, correct number of digits of precision, etc.).
Once you are able to successfully run the program with the correct output, you are ready to submit the program.
For this assignment, you only need to submit the source file
main.c to the Assignments tab in CougarVIEW.
(Note: Do not submit files with spaces in their names.)
Rubric:
Points Item
---------- --------------------------------------------------------------
_____ / 10 Documentation
Header comment block at the beginning of each file with:
+ Your full name
+ Date(s) code was written
+ Description
Comments explaining the role of each variable and major section of code
Correctness
Program solves the assigned problem using methods described in program description
_____ / 25 + Opens file and parses input file for loan terms (or displays an error message)
_____ / 10 + Calculates and displays the monthly payment amount
_____ / 20 + Displays interest, principal and balance for first and last 6 months
_____ / 0 Program compiles without errors
_____ / 0 Program executes without crashing
_____ / 65 Total
Notes
- Before writing any code, make sure that you can correctly calculate the monthly payment "by-hand".
- If you want to match the output exactly, download the linked files into the same directory as the executable then run the following in a terminal window:
gcc main.c -lm -o project1
./project1 < project1-stdinA.txt > output.txt
diff -w project1-stdoutA.txt output.txt
The second line, where we're running the executable project1, uses the contents of the project1-stdinA.txt in place of stdin (i.e., the user typing). That is what the "<" symbol means. Additionally, we're also redirecting stdout with the ">" symbol to go to output.txt (instead of the screen). This allows us to run it without having to type anything.
If the two files match exactly (which is what you want) then there should be NO output from diff. If diff shows one or more differences, fix them and run it again. To get side-by-side output (with the answer key on the left and your output on the right), replace the last line with:
diff -w --side-by-side project1-stdoutA.txt output.txt
For details about interpreting the output of diff, see the Using diff section on the Misc. webpage.
For the Zonda loan terms:
gcc main.c -lm -o project1
./project1 < project1-stdinB.txt > output.txt
diff -w project1-stdoutB.txt output.txt
Questions and Answers
- What happened to the inputted filename? How can I match the example if it's missing?
What we see in a terminal is both output (stdout) and input (stdin) weaved together. What is shown in the project webpage is just the output (stdout). When we type (stdin), that is not part of the output. So, if you’re using diff to compare output (which I will be), it would not take into account the input. So, prompt the user for the filename with the text provided in the example; read in the user’s response and then display the line that you’re processing the file and it will match the example.
- What's a ".tab" file?
In UNIX, the OS doesn't really care what the file extension is. ".tab" files are flat-text files, just like ".txt" files. This means that there's no special formatting or syntax to the file. It's just characters. If you're familiar if ".csv" files, they're the same thing, except that fields are separated by tabs instead of commas. I prefer ".tab" files over ".csv" files because tabs rarely appear in data, whereas commas are common.
Bonus:
Process multiple loan terms from a single file.