Today, we have a special guest blogger, **Philip Conrod**! He is sharing with us an excerpt from their book for kids, *Beginning Microsoft Small Basic *by Philip Conrod and Lou Tylee. You can find their books and other materials at the Computer Science For Kids web site. Thanks to Philip for sharing. Please enjoy!

==================

### A Simple Savings Calculator For Kids

In this program, we will build a savings account calculator. We will input how much money we can put into an account each month and the number of months we put money in the account. The program will then compute how much we saved.

#### Program Design

The steps needed to do this calculation are relatively simple:

- Obtain an amount for each month’s deposit.
- Obtain a number of months.
- Multiply the two input numbers together.
- Output the product, the total savings.

We will use the **ReadNumber** method to get user input. The **WriteLine** and **Write** methods will be used to output the savings amount. We’ll throw in an additional step to ask for the user’s name (an example of using the **Read** method).

#### Program Development

Start **Small Basic**. Click the **New Program** button in the toolbar. A blank editor will appear. Immediately save the program as **Savings** in a folder of your choice.

First, type the following header information and code that adds a window title:

1. '

2. ' Savings Program

3.

4. ' Beginning Small Basic

5. '

6. TextWindow.Title = "Savings Calculator"

We will use four variables in this program: one for the user’s name (**YourName**), one for the deposit amount (**Deposit**), one for the number of months (**Months**) and one for the total amount (**Total**). Type these lines to initialize the variables:

1. YourName = ""

2. Deposit = 0.0

3. Months = 0

4. Total = 0.0

Now, we start the code, using the steps outlined under Program Design. At any time, after typing some code, you might like to stop and run just to see if things are going okay. That is always a good approach to take. First, ask the user his/her name using this code:

1. ' ask user name

2. TextWindow.Write("Hello, what is your name? ")

3. YourName = TextWindow.Read()

Next, determine how much will be deposited in the savings account each month:

1. TextWindow.WriteLine("")

2. ' get deposit amount

3. TextWindow.Write("How much will you deposit each month? ")

4. Deposit = TextWindow.ReadNumber()

Notice the insertion of a blank line before printing the prompt. Finally, obtain the number of months:

1. TextWindow.WriteLine("")

2. ' get number of months

3. TextWindow.Write("For how many months? ")

4. Months = TextWindow.ReadNumber()

With this information, the total deposit can be computed and displayed using a WriteLine method:

1. TextWindow.WriteLine("")

2. ' compute and display total

3. Total = Deposit * Months

4. TextWindow.WriteLine(yourName + ", after " + months + " months, you will have $" + total + " in your savings.")

5. TextWindow.WriteLine("")

Save your program by clicking the **Save** button.

The finished code in the Small Basic editor should appear as:

1. '

2. ' Savings Program

3. ' Beginning Small Basic

4. '

5. TextWindow.Title = "Savings Calculator"

6. ' initialize variables

7. YourName = ""

8. Deposit = 0.0

9. Months = 0

10.Total = 0.0

11.

12.' ask user name

13.TextWindow.Write("Hello, what is your name? ")

14.YourName = TextWindow.Read()

15.TextWindow.WriteLine("")

16.' get deposit amount

17.TextWindow.Write("How much will you deposit each month? ")

18.Deposit = TextWindow.ReadNumber()

19.TextWindow.WriteLine("")

20.' get number of months

21.TextWindow.Write("For how many months? ")

22.Months = TextWindow.ReadNumber()

23.TextWindow.WriteLine("")

24.' compute and display total

25.Total = Deposit * Months

26.TextWindow.WriteLine(yourName + ", after " + months + " months,

27.you will have $" + total + " in your savings.")

28.TextWindow.WriteLine("")

#### Run the Program

Run your program. If the program does not run successfully, try to find out where your errors are using any error messages that may appear. We will cover some possible errors in the next class.

When the program runs successfully, you will see:

Type in your name, a deposit amount and a number of months. Your total will be given to you in a nicely formatted string output. Notice how the name, deposit, months and total are all put together (concatenated) in a single sentence, along with a dollar sign ($). Make sure the answer is correct. Remember, a big step in program design is making sure your program works correctly! If you say you want to save 200 dollars a month for 10 months and your computer program says you will have a million dollars by that time, you should know something is wrong somewhere!

When I tried the program, I got:

Notice if I deposit 403.52 (you don’t, and can’t, enter the dollar sign) for 11 months, the program tells me I will have $4438.72 in my savings account.

This program may not seem all that complicated. And it isn’t. After all, we only multiplied two numbers together. But, the program demonstrates steps that are used in every Small Basic program. Valuable experience has been gained in recognizing how to read input values, do the math to obtain desired results, and output those results to the user.

#### Other Things to Try

Most savings accounts yield interest, that is the bank actually pays you for letting them use your money. This savings account program has ignored interest. But, it is fairly easy to make the needed modifications to account for interest - the math is just a little more complicated. We will give you the steps, but not show you how, to change your program. Give it a try if you’d like:

- Define a variable
**Interest**to store the yearly savings interest rate. Interest rates are floating decimal numbers. - Add additional statements to allow the user to input an interest rate.

- Modify the code to use Interest in computing
**Total**. The code for that computation is (get ready - it’s messy looking):

Total = 1200 * (Deposit * (Math.Power((1 + Interest / 1200), Months) - 1) / Interest)

Make sure you type this all on one line – as often happens, the word processor has made it look like it is on two. As we said, this is a pretty messy expression, but it’s good practice in using parentheses and a mathematical function (**Power**). The number ‘1200’ is used here to convert the interest from a yearly value to a monthly value.

Now, run the modified program. Type in values for deposit, months, and interest. Make sure you get reasonable answers. (As a check, if you use a deposit value of 300, a months value of 14, and an interest value of 6.5, the total answer should be $4351.13. Note you’d have $4200 without interest, so this makes sense). Save your program.

I told a little lie, I didn’t get $4351.13 in the above example with interest. I actually got $4351.1272052172923076923076923!!!:

I rounded the answer. In such cases, the number should just be displayed with two numbers after the decimal. It is possible to do this using Small Basic but beyond the scope of our discussion at the moment.

### Summary

Notice the programs are getting a little more detailed as you learn more Small Basic. In this class, you learned about proper program design, mathematical functions and how to add input capabilities to your Small Basic programs. You built a little savings account program. And, an important concept to remember as you continue through this course is to always try to build your programs a few lines of code at a time. A good mantra is “code a little, test a little.” You will introduce fewer errors in your programs using this approach.

This chapter excerpt is adapted from the book *Beginning Microsoft Small Basic* by Philip Conrod and Lou Tylee.

To purchase this book in its entirety and to browse their other computer programming materials for kids, please see the Computer Science For Kids web site.

Excerpt © Copyright 2012 By Kidware Software LLC All Rights Reserved. Philip Conrod & Lou Tylee have co-authored dozens of books and tutorials for beginning Microsoft Basic, Small Basic, Visual Basic, and Visual C# developers of all ages for over 25 years.

that is very clever i am going to try it at home

Stephen, that's great!