Math

Overview

It is possible to perform most mathematical operations in your code. The most common operations are addition, subtraction, multiplication and division of variables. The following are some examples.

x = x + 1;             //adds 1 to x
z = z - 9;             //subtracts 9 from x
y = y * 4;             //multiplies x by 4
x = x / 7;             //divides x by 7

Exercise:

  1. Write a program that produces a column of numbers starting at 2 and increaseing by a factor of 2 i.e. 2, 4, 8, etc. Do not forget to include a short delay.

    TEACHER CHECK ____

  2. Modify your to produce a column of numbers starting at 9 and decreasing by total of 3.

    TEACHER CHECK ____

  3. Write a program that produces two columns of numbers based on two different variables. One column should start at 6 and count up by 2. The second column should start at 50 and count down by 2.

    TEACHER CHECK ____

  4. Write a program that produces two columns of numbers. The numbers in the first column can increase by any value you choose. The numbers in the second column should be twice the value of the numbers in the first column. This program should only use one variable. Remember from the previous lesson that you can perform a mathematical operation, (addition, subtraction, etc.) with a variable without changing its value.

    TEACHER CHECK ____