Qbasic Tutorial for beginners and children (Under Construction)


Qbasic Tutorial: Chapter 4

Solving Mathematical Problems (Part 1)

Solving simple mathematical problems is one of the easiest tasks that you could do in BASIC. If you are more keen at writing a game or making graphics, you will have to be patient.:) We'll deal about that later.

Before proceeding, you'll have to understand some Basic concepts:

What is a program?

A program is a set of instructions that makes the computer work.

In most programs you will have to write you will have to think as follows:

  • What data do I need to give to the computer?
  • What operations will the computer have to perform with the data given?
  • What results will the program display

There are three parts in every task that we accomplish 

Input --> Process --> Output

Input: What is needed

Process: What you need to do, calculate with the input

Output: The result obtained when the process has been done

 

Before beginning to code your program, it is important to write algorithms or pseudo codes in plain English that will outline what you want to do. 

Algorithms

 An algorithm is a set of precise instructions for solving a problem.  Algorithms can be represented in plain English or in form of flowcharts

Here's a simple algorithm for calculating the sum of 2 numbers

Get the first number

Get the second number

Calculate the sum

Display the sum

Let's illustrate this concept using some mathematical examples:

We'll be using colours to differentiate between the Input, Process and Output parts of the algorithm and program code.

Example 1: Finding the sum of 2 numbers

Write a program that will display the sum 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

Output on Screen

20

 

 

 

 

 

You will have noticed that this program is not very explicit. The following code makes the presentation more understandable as follows

Enhanced Example 1: Finding the sum of 2 numbers
Program Code

CLS

number1 = 8

number2 = 12

sum = Number1 + Number2

PRINT "This program finds the sum of two numbers"

PRINT "The first number is:"

PRINT number1

PRINT "The second number is:"

PRINT number2

PRINT "The sum is:"

PRINT sum

Output on Screen

This program finds the sum of two numbers"

The first number is:

8

The second number is:

12

The sum is:

20

 

 

 

 

 

The use of semi-colons in the PRINT statement tells BASIC to keep the cursor on the same line.

More Enhancement to Example 1: Finding the sum of 2 numbers
Program Code

CLS

number1 = 8

number2 = 12

sum = Number1 + Number2

PRINT "This program finds the sum of two numbers"

PRINT "The first number is:";

PRINT number1

PRINT "The second number is:";

PRINT number2

PRINT "The sum is:";

PRINT sum

Output on Screen

This program finds the sum of two numbers"

The first number is: 8

The second number is: 12

The sum is: 20

 

 

 

 

 

 

[ Previous Index Next ]


pages viewed since January  19 2002