COP 1220 (C Programming) Project #1
Due: Wednesday, May 30, 2001,
by the start of class
Purpose:
To practice basic C statements and programming ideas, such as if-statements,
loops, and simple input and output.
To practice good programming techniques, style, design, testing, and good
program documentation, on a small (about 70 lines of code) programming example.
Background:
A mortgage loan, or any loan, requires a schedule of payments.
Each payment represents some interest and some repayment of principal.
Although the amount of each payment is the same, the proportion of interest
to principal changes for each payment in a complicated way.
It is very useful therefore to have a program that can calculate and
print a schedule of loan payments.
(It is often a shock to discover how much interest will be paid,
often more than double the amount of the loan!)
Programming Requirements:
In this project, we want to print a schedule of payments for a mortgage
loan at a fixed rate of interest.
Although the method shown below is not the simplest, it will work and you must use it.
(Beside making the projects all based on the same formulas for easy and consistent grading,
you get lots of practice working with formulas and converting them into C!)
- Your program must prompt the user to enter the amount of the loan (a dollar amount),
the fixed annual interest rate, the number of payments (e.g., 30 years = 360 payments),
the starting month (a number from 1-12), and a starting year.
A good program will check the values input by the user to make sure they make sense:
no zero or negative values, a month number less than 13, etc.
- Avoid round-off error: use int where possible and use double
variables instead of float.
- The formulas below all use the monthly rate of interest.
The annual percentage that the user inputs must be converted:
divide the percentage by 100.0 to get a decimal,
then divide that by 12 to get a monthly rate.
(Short-cut: divide the user supplied value by 1200.0.)
- Calculate the monthly payment:
where pmt is the monthly payment,
amt is the amount of the loan (initial Present Value or initial balance
for you accountant types),
i is the monthly rate of interest, and
n is the number of payments.
Note the exponent in the calculation; you can use the function pow()
in the standard library (in <math.h>; see below for further information).
Note that the monthly payment is the sum of the interest paid that month,
and the principal repaid that month.
- Your program should now compute the amount of interest and principal for each payment,
and remaining balance after each payment.
The output should be in a tabular form (lined up in neat columns), with appropriate column headers.
These calculations are done in the following order:
- Calculate the new balance remaining:
where new_bal is the new balance remaining,
k is the payment number of this payment, and
i, pmt, and amt are the same as before.
- Calculate the principal that was repaid, which is the difference between
the old balance and the new balance:
principal = old_bal - new_bal
- Calculate the interest that was paid, the difference between the total monthly
payment and the principal that was repaid:
interest = pmt - principal
- Accumulate total amounts for each of: amount paid, interest paid, and principal paid.
- Update the old balance:
old bal = new bal
- Printout the totals at the bottom of the schedule (under the appropriate columns).
Creative Points Hint:
A proper schedule useful to lawyers and accountants would print yearly subtotals
for each calendar year.
Make sure not to print the totals twice if the last payment happens to be in December!
Use of pow() Function:
Calculating XY is easy with the pow() function.
The prototype for this function is:
double pow (double X, double Y);
Sample use: double z; z = pow(2.0, 3.0); /* now z equals 8.0 */
The arguments to pow can be arbitrary expressions.
Remember to include the <math.h> header file.
Sample Output:
A sample output is attached for a three year loan of $5000.00 at 8.0% annual interest
which starts in August 1994.
Your output should look similar to this when your program is run using the test data provided below.
The output shows the prompt messages my program prints (with user input shown in
boldface).
Then the actual output of the program is shown below that, which was redirected to the
file loan.out.
This is the output that you need to print out and turn in (not the user prompts).
Note that the program was compiled as a DOS executable in order to use I/O redirection.
However this was not necessary since it is possible to save and/or print the
output of a windows program.
Test Data:
You can (and should!) test your program with several different sets of data.
However, a printout of the following schedule must be turned in:
Amount: | 10000.00 |
Interest: | 8.5 |
Number of Payments: | 36 |
Starting Month: | 4 |
Starting Year: | 1998 |
To Be Turned In:
- A disk with your program on it. (loan.exe, loan.c)
(Borland project files are OK too.)
- A printout of the program.
- A printout of the result of running your program using the above test data.
Ask me for help if you get stuck!
Sample run of program with output: