Solving Mathematical Problems (Part 2)
As we say: Practice
makes perfect. Here are more examples:
|
Example 2 |
|
Write a program that will display the product of numbers 8 and 12
|
| Algorithm |
Program
Code |
Get
the first number
Get
the second number
Calculate
the sum
Write
down the result
|
number1
= 8
number2
= 12
sum
= Number1 + Number2
PRINT
sum
|
|
Be careful about not naming variables after BASIC reserved words. In the
example below, you might be tempted to use the word "base" for to
store the base of the triangle. The word "BASE" cannot be used. It is
a reserved word in BASIC
|
Example 3 |
|
Write a program that will calculate the area of a triangle of
base = 10 and height = 5
|
| Algorithm |
Program
Code |
Get
the base
Get
the height
Calculate
the area
Write
down the area
|
'The
word BASE is reserved in BASIC
'So we'll use
variable tbase to store the base
tbase
= 10
height
= 5
area
= 1/2 * tbase * height
PRINT
area
|
|
As it is the case in mathematics, operations in BASIC follows the BODMAS
(Bracket, Of, Division, Multiplication, Addition,
Subtraction) sequence. In the example below, BASIC will calculate the sum
of the length and breadth within brackets then multiply it by 2.
|
Example 4 |
|
Write a program that will calculate the perimeter of rectangle
of Length = 20 and breadth = 8.
|
| Algorithm |
Program
Code |
Get
the length
Get
the breadth
Calculate
the area
Write
down the area
|
length
= 20
breadth
= 8
area
= 2 * (length + breadth)
PRINT
area
|
|
There may be several ways to process a task in BASIC. Use the one that suits
you better.
|
Example 5 (method 1) |
|
Write a program that will calculate the area of a circle of
radius = 7.
|
| Algorithm |
Program
Code |
Get
radius
Get
pi
Calculate
the area
Write
down the area
|
radius
= 7
pi
= 22/7
area
= pi * radius * radius
PRINT
area
|
|
|
Example 5 (method 2) |
|
Write a program that will calculate the area of a circle of
radius = 7.
|
| Algorithm |
Program
Code |
Get
radius
Get
pi
Calculate
the area
Write
down the area
|
radius
= 7
pi
= 22/7
area
= pi * (radius ^ 2)
PRINT
area
|
|
[ Previous Index
Next ] |