CS 2073, Spring 2006
 Program 3
 Compound Interest
    Week 3: Jan 30 - Feb 3
 Due (on time): 2006-02-10  23:59:59
 Due (late):        2006-02-13  23:59:59

Program 3 must be emailed to: nrwagner@cs.utsa.edu
following directions for: running and submitting a C program, with deadlines:
  • 2006-02-10  23:59:59 (that's Friday, 10 February 2006, 11:59:59 pm) for full credit.
  • 2006-02-13  23:59:59 (that's Monday, 13 February 2006, 11:59:59 pm) for 75% credit.


Introduction: You are to write three similar programs for this assignment:

  1. The first program asks you to calculate the result of applying compound interest. Specifically, start with an amount of money, say a double amount, with a yearly interest rate, say a double rate, and with a number of years to compound the interest, say an int years. The program should apply the interest at the given rate to the given amount, at the end of each year, for the given number of years.

  2. The second program has an additional integer variable compound that says how many times to compound the interest per year.

  3. The third program asks you to iterate the calculations of the second one in a certain way that is specified below.

Details about what to do:
  1. For the first program:
    1. Read values for variables amount, rate, and years. (The first two are doubles and the third is an int, so you need to be careful with the formatting codes in scanf.)
    2. Use a for loop to apply interest at the end of each year. Print the amount at the end of each year as shown in the next section below. (Be careful: if the rate is read in as a percent, then you will have to divide it by 100.0 before using it.) Note that we are studying programming here and not finance. There are formulas that give the result here without using a loop, but much of the reason for doing this program is to learn how to write loops. You are not allowed to use a simple formula for the results asked for here, and you are not allowed to use the pow function in this program.

  2. For the second program:
    1. Modify the first program so that you also read in a value for an integer variable compound, representing the number of times per year to compound the interest.
    2. Thus you will need to apply the interest at a rate equal to rate/compound, and you will have to apply it years*compound many times.
    3. Finally (the hardest part), only print the result at the end of a year, that is, after applying interest compound many times. See below for sample output.

  3. For the third program:

    1. Start the second program with amount = 1.0, rate = 100, years = 1, and compound = 1, using simple assignments, instead of reading in. This means that we are investing 1 dollar at 100% interest for 1 year, compounded once. The result should be 2 dollars.

      Next try compound = 12, leaving the other numbers the same. This means that we are investing 1 dollar at 100% interest for 1 year, but compounding the interest every month. The result should be 2.613035290224 dollars, at 12-digit accuracy.

      Next try compounding weekly, that is, compound = 52
      Next try compounding daily, that is, compound = 365
      Next try compounding hourly, that is, compound = 8760
      Next try compounding each minute, that is, compound = 525600
      Next try compounding each second, that is, compound = 31536000

    2. Now write a single program that does a calculation similar to the multiple runs in part (a) above, except that it should use the following values for successive values of compound: 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000. Print the result of using each successive larger value of compound with at least 12 digits to the right of the decimal place. (For each successive value of compound, your program should print just a single number, along perhaps with the value ofcompound.) As a hint, here is a for loop that makes the variable compound take on these values. Your loop that calculates the compound interest should be buried inside the curley brackets after this loop.

        for(compound = 1; compound <= 1000000000; compound = compound*10) {
           // calculate compounded interest here
        }
        


Sample input and output: Here are some sample outputs:

  1. For the first program:

    Notice that at the end of the first year, 100.0 interest is added to the 1000.0 you started with (10% of 1000.0). Then in the second year, the interest added is 110.0 (10% of 1100.0). And so forth.

  2. For the second program:

    Notice that here the program is applying 2.5% interest 40 times. It is only printing every fourth time around the loop. The result after 10 years is somewhat bigger, but not too much (91.32 bigger).

  3. For the third program, here is just the start of possible output (but the numbers are correct). Your program should continue with lines for 1000, 10000, etc.


What you should email: Refer to the submissions directions and to deadlines at the top of this page. The text file that you submit should first have Your Name, the Course Number, and the Program Number. The rest of the file should have the following in it, in the order below, and clearly labeled, including at the beginning the appropriate item letters: a, b, c, etc.

 Contents of email submission for Program 3:

Last Name, First Name; Course Number; Program Number.

  1. The C source program for Program 1 above, along with two runs:
    the run shown above, and a run with 2500 invested for 5 years at 20% interest.
  2. The C source program for Program 2 above, along with two runs:
    the run shown above, and a run with 2500 invested for 5 years at 20% interest,
    and compounded each month (compound = 12).
  3. The C source program for Program 3 above, with its single run.
    In particular you should get the result of 1 dollar invested at 100% interest
    for 1 year and compounded 1000000000 times. (This may take a few minutes
    to run. If you are using a computer from the Stone Age, that is, several years
    old, you may need to leave off this last value.)
  4. Does the final resulting number look familiar to you? What do you think is
    going on here? What would it mean to compound instantaneously?


Revision date: 2006-02-02. (Please use ISO 8601, the International Standard.)