# Number Bases and Basic Programming for Junior Secondary Students

## Number Bases

This article teaches basic computer problem-solving skills for beginners. It begins with number bases, where decimal, binary, octal, and hexadecimal number conversions were treated. It further discusses the fundamentals of BASIC programming language for beginners.

## Decimal Numbers

In number bases, decimals are our natural way of writing numbers. We grew up to learn how to read and write numerals as 0, 1, 2, … 99, 100, … 250, …, numbers expressed in this fashion are traditionally called decimal numbers. However, numbers can also be written in other bases other than base 10, such bases include binary or base 2, octal or base 8 and hexadecimal or base 16. When numbers are written on other bases, it is important to specify the base on which the said number is written.

## Binary Numbers

Binary numbers are numbers written in base 2. In number bases, the binary system is internally by digital electronics and all computer-based devices. It represents a number of 0 and 1 notations.

### How to convert from decimal to binary

To convert from a number in base 10 to a number in base 2, simply divide the given decimal number by 2. For example, convert 9 to a number in base 2.

Solution

9 ÷ 2 = 4      R       1                 (dividing 9 by 2 gives 4 remainder 1)

4 ÷ 2 = 2      R       0                 (dividing 4 by 2 gives 2 remainder 0)

2 ÷ 2 = 1      R       0                 (dividing 2 by 2 gives 1 remainder 0)

1 ÷ 2 = 0      R       1                 (dividing 1 by 2 gives 0 remainder 0)

The remainders are then written from down to up (i.e. starting from the last remainder to the first remainder) we have:

9 = 10012

Convert the following decimal numbers to a number in base 2. (a) 5    (b) 55 (c) 80 (d) 111          (e) 16

(a) 101        (b) 110111   (c) 1010000 (d) 1101111 (e) 10000

### How to convert from binary to decimal

In this case, you will assign power (starting with y … 2, 1, 0, …) to the binary number from the left. Where y is the count number of the binary number minus 1. For example, convert 1011102 to a number in base 10.

Solution

15      04      13      12      11      00

(here, y = 6 – 1 = 5; assign powers 5, 4, 3, 2, 1, 0, from the left)

Multiply each entry by 2 raised to the assigned power and sum:

1×25 + 0x24 + 1×23 + 1×22 + 1×21 + 0x20

Multiplying out the powers and summing the above we have:

1×32 + 0x16 + 1×8 + 1×4 + 1×2 + 0x1

(note that 20 = 1)

32 + 0 + 8 + 4 + 2 + 0 = 46

Therefore, 1011102 = 46

## How to convert decimal number to octal number

Octal number is a number expressed in base 8. In number bases, to convert from decimal number to octal, follow the same method used when converting decimal to binary, but divide by 8 instead of 2. For example, convert 78 to a number in base 8.

Solution

78 ÷ 8 = 9    R       6                 (dividing 78 by 8 gives 9 remainder 6)

9 ÷ 8 = 1    R       1                 (dividing 9 by 8 gives 1 remainder 1)

1 ÷ 8 = 0    R       1                 (dividing 1 by 8 gives 0 remainder 1)

Therefore, 78 = 1168

## Octal Numbers

Octal numbers are numbers in base 8.

### Converting from base 8 to decimal

To convert from base 8 to a number in base 10, follow the same method used in converting from base 2 to base 10, however, multiply by 8 instead of 2. For example, convert 5618 to a number in base 10.

Solution

52      61      10

(Here, y = 3 – 1, assign 2, 1, 0)

5×82 + 6×81 + 1×80 = 5×64 + 6×8 + 1×1 = 320 + 48 + 1 = 369

Therefore, 5618 = 369

In number bases, Hexadecimal or simply Hex, is a number system represented in base 16. The decimal system can represent only 10 digits while the hex system has 16 digits. In hex, the number bases system is represented as 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F. The hex number system is mostly used in information technology. For example, it can be used to represent color in HTML programming language: white is represented by FFFFFF while black is represented by 000000.

### How to convert from decimal to hexadecimal

To convert from decimal to hex, follow the same principle used when converting from decimal to binary, but divide by 16 instead of 2. After you have divided a decimal number by 16, use the table below to represent the remainder.

### The decimal to hexadecimal conversion chart

 Decimal 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Hex 0 1 2 3 4 5 6 7 8 9 A B C D E F

For example, convert 9210 to a number in base 16.

Solution

92 ÷ 16 = 5 R       12      (dividing 92 by 16 gives 5 remainder 12)

5 ÷ 16 = 0 R       5        (dividing 5 by 16 gives 0 remainder 5)

From the decimal to hex conversion table, 12 = C.

Therefore, 9210 = 5C16

## How to convert from hex to decimal

To convert a hex number to decimal, follow the same principle used in converting from binary to decimal. For example, convert 6D116 to a number in base 10.

Solution

6        D       1

(Note: D = 13; y = 3 – 1 =2, hence, assign 2, 1, 0)

We therefore have:

62      131    10 = 6×162 + 13×161 + 1×160 = 6×256 + 13×16 + 1×1 =

1536 + 208 + 1 = 1745

Therefore, 6D116 = 174510

## Using the Computer to convert Decimal, Binary, Octal and Hex

You can easily use the calculator application in your computer to convert from decimal to binary, octal or hex number systems and vice versa. Simply do the following.

1. Open the calculator application (type calc in the search box or type calc in the Run box and press OK)

The calculator application will open as shown below.

• In the calculator window, click View and from the drop-down menu select Programmer or simply press Alt+3. The calculator application changes to the window shown below.
• On the window, select the number system you want and type in the number you want to convert.
• After typing in the number in the given base, click select the base you want to convert the number to. For example, select Hex and type in 5E, then select Dec, your 5E will become 94.
• Do the same for each of the numbers you want to convert by changing from any of Dec, Hex, Bin or Oct.

# UNITS OF STORAGE IN COMPUTER

## Units of Storage

Computers store data in different storage devices such as a Hard disk, RAM, flash drive, etc. Data are stored in these devices in binary format (0s and 1s). In order to measure the capacity of the different storage devices, a unit of measurement becomes necessary.

In mathematics and all works of life, there are different units of measurement. For example, length and distance are measured in meters, weight, and mass is measured in grams, the liquid is measured in liters, and time is measured in minutes. In each of these items, there are different by identical units of measurement to ascertain the actual quantity. Length has varying units such as millimeter (mm), centimeter (cm), etc; weight, following similar pattern has milligram, centigram, etc. In this case, millimeter and milligram are the smallest units of measuring length and weight respectively.

Since these items can be accurately measured, it is necessary that data be measured in like manner. In such case, we have different units of measuring data, namely: byte, kilobyte, megabyte, gigabyte, and terabyte.

### Bit:

Bit is the short form for a Binary digit (0 or 1) and is the smallest unit of data in computer and electronic communication. Computers are designed to execute instructions in multiples of bits. A bit can be viewed as a mechanical switch, which can be either switched on or off.

### Nibble:

A nibble is four binary digits, it contains four bits (0010) or half an octet.

### Byte:

A byte is the smallest unit of measuring the size of a memory. It can store one character at a time. A byte is made up of 8-bits or an octet or 2 nibbles, i.e. 10110101 makes up a byte. With 8 bits you can store any number between 0 and 255. Bytes are measured in powers of 2 as follow:

210 = 1024 = Kilobyte

220 = 10242 = 1048572 = Megabyte

230 = 10243 = 1073741824 = Gigabyte

240 = 10244 = 1099511627776 = Terabyte

A kilobyte can be expressed as one thousand bytes, megabyte expressed as one million bytes, gigabyte as one billion bytes and terabyte as one trillion bytes.

### Word:

A word is the number of bits that are manipulated as a unit by the particular CPU of the computer. It varies depending on the computer architecture. Data is fetched from memory to the processor in word size chunks and manipulated by the ALU in word size chunks. A computer architecture can be 16-bit, 32-bit or 64-bit. A word contains two bytes or four nibbles. For a computer with a 16-bit architecture, word is 2 bytes or 4 nibbles. For a 32-bit architecture, word is 4 bytes or 8 nibbles. This can also be called a double word.

### Difference between kilometer, kilogram, and Kilobyte

A kilometer is a metric system unit of measuring length and distance. Kilo means 1000 of, hence kilometer means 1000 meters. Similarly, the kilogram is 1000 grams, a standard unit of measuring weight while kilobyte is 1000 bytes, a standard for measuring data storage.

## Conversion from byte to kilobyte, megabyte, gigabyte and terabyte

Before conversion, it is important to understand that 1024 bytes (b) make 1 kilobyte (Kb), 1024 (kb) make 1megabyte (MB), 1024 (MB) make 1 gigabyte (GB) and 1024 (GB) make 1 terabyte (TB).

To convert from bytes to KB, MB, GB or TB, we simply divide the given size by 1024 at each level.

For example, convert 10,256,879bytes to gigabyte.

Solution

We shall move from bytes to KB to MB and to GB

10256879 ÷ 1024 = 10016.48 KB

10016.48 ÷ 1024 = 9.781 MB

9.781 ÷ 1024 = 0.00956 GB

Therefore, 10256879 bytes = 0.00956GB

Example 2: Convert 2,789,650 KB to Gigabyte

Solution

2789650 ÷ 1024 = 2724.27 MB

2724.27 ÷ 1024 = 2.66 GB

## Conversion from TB to GB, MB, KB, and bytes

To convert from TB to GB, MB, KB, and bytes, we simply multiply the given size by 1024 at each level.

For example, convert 1.66GB to kilobytes.

Solution

We shall move from GB to MB to KB

1.66 x 1024 = 1699.84MB                 (multiply by 1024 to get MB)

1699.84 x 1024 = 1740636.16KB       (multiply by 1024 to get KB)

Therefore, 1.66GB = 1,740,636.16KB

Example 2: If the size of a diskette is 1.44MB, then how many characters can a diskette store?

Solution

1 character = 1 byte, therefore, the total number of bytes = total number of characters.

To solve this problem, we shall move from MB to KB to B

1.44 x 1024 = 1474.56KB

1474.56 x 1024 = 1,509,949.44 bytes

Therefore, a diskette can store 1,509,949.44 characters.

# COMPUTER PROBLEM SOLVING SKILLS

## Computer Program

A computer program is a list of commands or instructions that tell a computer what to do. It can be defined as a sequence of instructions, written by a programmer or group of programmers using a programming language to perform a specified task with a computer.

A computer program when installing in a desktop computer, enables computer users to perform specific tasks on the computer. There are thousands of computer programs in the market and on the internet, each designed to achieve a definite purpose. Some of these programs are free while some are designed for commercial purpose.

Examples of a computer program include Microsoft office suite (Word, Excel, Powerpoint, Outlook, Infopath, etc), CorelDraw, AutoCAD, Mozilla Firefox, Safari, Dreamweaver, etc.

## Computer Programming Language

A programming language is a computer program used to write source codes that tell the computer what to do. Programming languages are used by programmers to develop all computer programs and computer software.

Computer programming is the process of writing or editing source code (a collection of instructions for the computer to follow). A programmer is someone who writes source codes i.e. who develops a computer program.

The following are examples of programming language: LOGO, BASIC, C, C++, C#, JAVA, PASCAL, COBOL, Visual Basic, Fortran, etc.

# BASIC

## BASIC Language

### Meaning of basic

BASIC is an easy general purpose and high-level programming language for beginners. The acronym BASIC stands for Beginners All-purpose Symbolic Instructions Code. BASIC was designed by John Kemeny and Thomas Kurtz on May 1, 1964.

### BASIC Character Set

A character, in computer language, is any symbol that can be stored in one byte. A character set is a list of defined characters recognized by a computer hardware and software. The BASIC character set is a list of characters recognized by the BASIC programming language. In BASIC, there are 3 types of characters, namely:

Alphabetic characters: This includes the standard English alphabets A, B, C, …, X, Y, Z.

Numeric characters: This includes the standard numbers 0, 1, …, 9. When using numeric data in a BASIC program, you must adhere to the following conventions. Do not use “\$” (dollar sign), ‘‘,” (comma), or the “/” (slash) in a BASIC number.

Special characters: The special characters used in BASIC include:

 Symbol Interpretation Symbol Interpretation Blank ’ Single quotation mark = Equal sign or assignment symbol ” Double quotation mark + Plus sign ; Semicolon – Minus sign : Colon * Asterisk or multiply symbol ! Exclamation symbol / Slash or divide symbol ? Question mark ^ Up-arrow or exponentiation symbol & Ampersand ) Right parenthesis < Less than symbol ( Left parenthesis > Greater than symbol , Comma # Number or pound sign . Point or period \$ Dollar sign >= greater than or equal % Percent sign <= less than or equal <> not equal ** exponentiation

## Key BASIC Statements

Statements are used when programming in BASIC. Some of these statements include the Line number, Remark, Assignments, Output, and Programme Terminator.

### Line Number

A line number is used in BASIC to create a sequence. The line number is important in BASIC because BASIC is an unstructured programming language. It helps the programmer to provide the targets of branching in the programme sequence.

For example:

10      If y = 15 GOTO 40

20      y = y + 5

30      GOTO 10

40      PRINT “y is 15”

The numbers 10, 20, 30 and 40 are line numbers.

### Remark (REM)

A remark or simply REM statement is used to provide a programmer’s comment in the program sequence. It is a non-executable statement which gives an explanation of the program being written. When a statement with the keyword REMARK is used, the entire line on which the statement appears is considered a comment.

For example

10      REMARK   This program is used to add two numbers together

20      REMARK   in order to arrive at an answer

30      INPUT        x

From the above, each line that begins with remark is ignored during programme execution. As can be seen, the remark statement gives a clear description of what the programme is designed for. It gives readability and helps to document and test the programme at a later time.

### Assignments

In BASIC, an assignment statement gives a value to a variable. A variable is an item that does not have a particular value, that is, the value can change. There are three key assignment statements used in BASIC, namely: LET, INPUT and DATA.

Note that the equal sign (=) is used to assign a value to a variable.

### LET statement

The LET statement is used to assign a constant value to a variable name, a variable to a variable name, or the result of an expression to a variable name.

Examples

10      LET   X = 15         (Assign a constant to a variable name)

20      LET  Y = Y + X   (Assign a result of the expression to a variable)

30      LET  Z = Y          (Assign a variable to a variable name)

### INPUT statement

The INPUT statement is used to collect information or data from the programme user. It is designed so that the programme user can enter the requested information during the programme execution.

For example, This simple program is used to calculate the average of the values entered by a user.

10      REMARK   This program calculates the average of 5 numbers

20      REMARK   entered by a user

30      PRINT “Please ENTER five numbers, separated by commas”

40      REMARK   The next line is used to collect the numbers

50      INPUT        num1, num2, num3, num4, num5

60      REMARK   The next line is used to compute the average

70      Avg = (num1 + num2 + num3 + num4 + num5) / 5

80      PRINT “The average of your five numbers is: ”; Avg

### DATA statement

The DATA statement provides a convenient way to insert data into programs. It is used to store both numeric and string values so that these values can be accessed using the READ statement in a BASIC program. Note the following when using the DATA statement:

1. Constantsare numeric constants in any format. No expressions are allowed in the list.
2. Single string statements should not be enclosed in quotation marks. You can enclose strings in quotation marks if they contain commas, colons, or significant leading or trailing spaces.
3. DATA statements are not executable. They may be placed anywhere in the program. A DATA statement can contain as many constants that will fit on a line (separated by commas), and any number of DATA statements may be used in a program.
4. READ statements access the DATA statements in order (by line number). The READ statement read the data contained in the DATA statement as one continuous list of items not minding how many items are on a line or where the lines are placed in the program.
5. The variable type (numeric or string) given in the READ statement must agree with the corresponding constant in the DATA statement, or a “Type Mismatch” error occurs. That is if an entry in a DATA statement is “Staff”, the corresponding variable in the READ statement must be a string.
6. DATA statements may be reread from the beginning by use of the RESTORE statement.

Example

CLS

10      PRINT        “Country”, “State”, “City”, “Postcode”

20      DATA         Nigeria, Abia, Umuahia

30      DATA         440002

40      READ         A\$, B\$, C\$, D

50      PRINT        A\$, B\$, C\$, D

### OUTPUT statement

The output statement is simply used to display results on the screen. The major output statement used in BASIC is the PRINT statement. In the above examples, we have illustrated the use of the PRINT statement.

### Programme Terminator

In BASIC, programme terminator is used to terminate the program execution. There are two statements that can be used: STOP statement and END statement.

### STOP statement

STOP statement is used to terminate program execution and return to command level. It can be used anywhere in a program to terminate execution. When a STOP is encountered, the following message is printed: “Break in ‘line number’”. The STOP statement does not close files. You can continue program execution using CONT command after a STOP statement is encountered.

### END statement

END statement is used to terminate program execution, close all files, and return to command level. It does not display “Break in ‘line number’” when encountered and it can be placed anywhere in a program to terminate program execution. When writing a program, it is optional to use the END statement.

## Examples of Simple Program

200    LET A = 25

300    LET B = 6

400    LET C = A + B

500    PRINT C

600    END

• 10      REM Find the perimeters of rectangles

20      PRINT “Type in two numbers separated by commas”

30      INPUT X, Y

40      IF A >= 50 GOTO 90

50      LET Z = 2*X + 2*Y

60      PRINT “SIDE”, “SIDE”, “PERIMETER”

70      PRINT X, Y, Z

80      GOTO 20

90      END

• 10      REM Find the sum of any 5 numbers

20      PRINT “Type any 5 numbers separated by comma”

30      INPUT A, B, C, D, E

40      LET S = A + B + C + D + E

50      PRINT “Sum of Numbers is:”; S

60      END