Let’s Make Money
Let's say you invested $100 in a project that yields 2% at regular periods of time. The Table shows how your money grows over time.
Table: How your money accumulates
After 
You’ll have 
Also equals 
1 period 
100 + (0.02 ´ 100) = 102 
100 ´ (1+0.02)^{1} 
2 periods 
102 + (0.02 ´ 102) = 104.04 
100 ´ (1+0.02)^{2} 
3 periods 
104.04 + (0.02 ´ 104.04) = 106.12 
100 ´ (1+0.02)^{3} 
… 


This is the idea of compound interest; you gain interest on the interest! (Surprise your parents by asking them if they’re compounding their interest!) The starting amount (the $100 in this example) is the principal. How often the interest is compounded is up to the bank! For example, they can do it four times a year (called quarterly compounding), 12 times a year (called monthly compounding), or even 365 times a year (called daily compounding).
In this example, you’ll write a program that calculates the compound interest on a principal (P) at an annual interest rate (R). The interest is compounded quarterly for Y years. The complete program’s in the Listing. Save the program as CompoundInterest.sb.
Listing: Using a For loop to track compound interest
1 ' CompoundInterest.sb
2 ' Computes the compound interest
3
4 P = 4000 'Principal = $4000
5 Y = 10 '10 years
6 R = 0.04 'Annual Interest rate = 4%
7 R = R/4 'Interest rate per quarter
8
9 For N = 1 To (Y * 4) 'Loops for the number of quarters
10 I = R * P 'Calculates the interest this quarter
11 P = P + I 'Adds the interest to the principal
12 EndFor
13
14 P = Math.Round(P) 'Rounds the answer
15 TextWindow.Write("You'll have $" + P)
16 TextWindow.WriteLine(" after " + Y + " years.")
The program starts by initializing the values of P, Y, and R (Lines 46). The short variable names in this example are okay because they don’t affect the program’s clarity. In Line 7, the program divides the interest rate by 4 to get the interest rate per period (for each quarter). The program then starts a loop than runs for (Y*4) periods, because the interest is compounded quarterly (Line 9). In each iteration, the program calculates the interest and adds it to the principal (Lines 1011). When the loop ends, the program rounds the answer (Line 14) and displays the result (Lines 1516):
You'll have $5955 after 10 years.
TIP: Although we usually set the loop parameters to constants values, the initial and terminal values in a For loop can be any Small Basic expression. Line 9 uses the expression (Y * 4) as the For loop’s terminal value.
TRY IT OUT!
Update the Listing to read the interest rate from your user. Run the program for these interest rates: 3%, 3.5%, 4.5%, 5%, and 6%. If you were able to invest your $4000 at 6% interest rate, can you find out how many years it would take you to become a millionaire? Hint: try different iteration counts in the For loop until your money exceeds $1,000,000.
Leave a comment if you have any questions!
Head to http://blogs.msdn.com/SmallBasic to download it and learn all about it!
Small and Basically yours,
 Ninja Ed & Majed Marji
Computers Today (part 1 of 6)
blogs.msdn.com/.../computerstoday.aspx
.....
CS SPOTLIGHT: Girls in computer programming... why it matters!!!
blogs.msdn.com/.../csspotlightgirlsincomputerprogrammingwhyitmatters.aspx
...
Computational Thinking  Videos & Papers by Jeannette Wing
blogs.msdn.com/.../computationalthinkingvideosamppapersbyjeannettewing.aspx