Program 2

Home Charlie Poole Contact Info Search Contents

Program 2 - January 17

Up

Basic Control Structures and Expressions

Step-by-step procedures can always be composed out of the three basic control structures: sequence, conditional branches and conditional (while) loops. We will learn of other control structures later but they only make certain programs more convenient to develop. With these three structures any procedural algorithm, no matter how complex, may be coded.

For this assignment, you will use the three basic structures to produce a program that displays the amortization table for a loan. Input to the program will be the amount of the loan, the interest rate per period and the amount paid each period. Payments are made at the end of each period until the full loan is paid off. Out of each payment, the interest for the period is first paid. Then the remainder of the payment is used to reduce the principal amount. The formula to calculate interest due for a period is:

interest due = principal at start of the period x interest rate

The formula for calculating the amount of the payment applied to the principal is

principal reduction = payment amount - interest due

The formula for calculating the balance at the end of the period is

new balance = principal at start of the period - principle reduction

Your program should begin with your standard comment block including your name as the author and a description of what it does. In addition, you should describe the overall program structure in a sentence or two so that someone reading it knows what to look for. Further requirements and options are described in the following sections.

Input

You tell the user what the program does and prompt for the necessary information. At this point, we have not yet learned enough to completely validate the input, so it's OK to assume that the user will enter a number if you ask for one.

You should make validation checks on the amounts entered: none of them can be negative and the payment must be large enough so that the loan actually gets paid down. If something is wrong, print an error message and quit.

Optionally, you may ask for the user's name and display it later in your output.

Optionally, you may allow the user to re-enter invalid input rather than just ending the program.

Optionally, you may allow the user to change the values entered and rerun the problem after you have displayed the results.

Processing

You may be surprised to see that this is the shortest part of the program. There are various ways to organize the calculation and you may need to declare a number of variables. Look at what variables are needed in the output section to decide how to do the calculation. 

Your processing and output should both take place in a while loop. Think about the most convenient way to terminate the while loop, ideally using a variable that you have to calculate anyway. Make sure you specify a condition that is guaranteed to terminate the loop at some point.

You will need to deal specially with the last payment. The amount of this payment will have to be only what is needed to pay the interest and reduce the principal balance to zero.

Output

Produce a neatly formatted list for each period. Each period's information should be displayed on a single line. You should show the beginning balance, payment amount, interest for the period, amount that went to the principal and the new balance.

Optionally, print headings for each column and make the numbers all line up correctly.

You have to deal with the special case of the last payment

 

Up

Home Charlie Poole Contact Info Search Contents

Copyright © 2000  Charlie Poole. All rights reserved.
Revised: July 15, 2002 - cpoole@ctc.edu