0% found this document useful (0 votes)
2 views

Introduction to Computers Lecture notes

The document provides a comprehensive introduction to computers, detailing their operations, advantages, limitations, and components, including input and output devices, memory types, and the central processing unit (CPU). It also covers hardware and software classifications, programming languages, and the different types of computers based on purpose and capacity. Additionally, it introduces numbering systems and their conversions, emphasizing the binary and decimal systems.

Uploaded by

reza goldaran
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Introduction to Computers Lecture notes

The document provides a comprehensive introduction to computers, detailing their operations, advantages, limitations, and components, including input and output devices, memory types, and the central processing unit (CPU). It also covers hardware and software classifications, programming languages, and the different types of computers based on purpose and capacity. Additionally, it introduces numbering systems and their conversions, emphasizing the binary and decimal systems.

Uploaded by

reza goldaran
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

CHAPTER 1

INTRODUCTION

What is a computer?
A Computer is an electronic device that can perform activities that involve
Mathematical, Logical and graphical manipulations.
It performs the following three operations in sequence.
1. It receives data & instructions from the input device.
2. Processes the data as per instructions.
3. Provides the result (output) in a desired form.

Data: It is the collection of raw facts, figures & symbols.


Ex: Names of students and their marks in different subjects listed in random
order.
Program: Set of instructions that enables a computer to perform a given task.
Information: It is the data that is processed & presented in an organized
manner. (Results)
Ex: When the names of students are arranged in alphabetical order, total and
average marks are calculated & presented in a tabular form, it is information.

Advantages of computers
1. High speed: Computers have the ability to perform routine tasks at a greater
speed than human beings.
2. Accuracy: Computers are used to perform tasks in a way that ensures
accuracy.
3. Storage: Computers can store large amount of information.

Limitations (disadvantages) of computers


1. Computers need clear & complete instructions to perform a task accurately.
2. Computers cannot think.
3. Computers cannot learn by experience. (Questionable: artificial intelligence)

1
Generations of computers:
Generation Component used
First Generation Vacuum tubes
(1946-1954 )
Second Generation Transistors
(1955-1965)
Third Generation Integrated Circuits (IC)
(1968-1975 )
Fourth Generation Very Large Scale Integrated Circuits
( 1976-1980) (VLSIC)
Fifth Generation Ultra-Scale Integrated Circuits (ULSIC)
(1980 – till today ) Micro Processor (SILICON CHIP)

Anatomy of Computers
The computer system consists of three units:
1. Input device
2. Central Processing Unit (CPU)
3. Output device

Block diagram of a Computer

2
Units and Their Functions
1. Input device: Reads information from input media and enters to the computer
in a coded form.

2. CPU
(a) Memory unit: Stores program and data
(b) Arithmetic Logic unit: Performs arithmetic and logical operations
(c) Control Unit: Interprets (translate from high level to low level) program
instructions and controls the input and output devices

3. Output device: decodes information and presents it to the user

Central Processing Unit


It is the part of the computer that carries out the instructions of a computer
program. It is the unit that reads and executes program instructions. Hence it is
known as the “brain” of the computer.
The CPU consists of Storage or Memory Unit, Arithmetic Logic Unit (ALU) and
Control Unit.

(a) Memory Unit: It is also known as the primary storage or main memory. It
stores data, program instructions, internal results and final output temporarily
before it is sent to an appropriate output device.

(b) Arithmetic and Logical Unit (ALU): It is the unit where all Arithmetic
operations (addition, subtraction etc.) and logical operations (>, <, AND, OR etc.)
are performed. Logic operations include any operations that manipulate
Boolean values. Boolean values are either true or false. Intermediate generated
results in ALU are temporarily placed in memory until needed at later time. Data
may move from primary memory to ALU and back again to storage many times
before the process is finalized.

(c) Control Unit: It acts as a central nervous system and ensures that the
information is stored correctly and the program instructions are followed in
proper sequence as well as the data are selected from the memory as necessary.
It also coordinates all the input and output devices of a system.

Input Devices
Devices used to provide data and instructions to the computer are called Input
devices. Some important input devices are:

3
Key board, Mouse, Scanner, Web camera, Microphone, Barcode reader, Flash
Disk (?) etc.

Output devices
Any device that is capable of representing information on a computer is called
an Output device. Output devices receive information from the CPU and present
it to the user in the desired form.
Some important Output devices are: Monitor, Printer, Plotter, Projector,
Speakers, Headphone, etc.

Memory of the Computer


Memory or storage capacity is one of the important components of a computer.
Any storage unit of a computer system is classified on the basis of the following
criteria:
1. Access time: This is the time required to locate and retrieve stored data from
the storage unit in response to program instructions.
2. Storage capacity: It is the amount of data that can be stored in the storage
unit.
3. Cost per bit of storage.

Units of Memory
The computer stores a character in the storage cells with binary (0, 1)
mechanism. Thus the basic unit of memory is a bit (binary digit). A bit has a single
binary value, either 0 or 1.

Byte
A group of 8 bits is called byte. A byte is the smallest unit, which can represent
a data item or a character.

The following table lists some higher storage units


1 Kilobyte (KB) 1 KB = 1024 Bytes
2 Megabyte (MB) 1 MB = 1024 KB
3 Gigabyte (GB) 1 GB = 1024 MB
4 Terabyte (TB) 1 TB = 1024 GB
5 Petabyte (PB) 1 PB = 1024 TB

There are really 1024 bytes in a kilobyte. The reason for this is because
computers are based on the binary system. That means hard drives and memory
are measured in powers of 2. For example,

4
20 = 1
21 = 2
22 = 4
23 = 8
24 = 16
25 = 32
26 = 64
27 = 128
28 = 256
29 = 512
210 = 1024
Notice how 210 is 1024. Therefore, 210, or 1024 bytes compose one kilobyte.
Furthermore, 1024 kilobytes compose one megabyte, and 1024 megabytes
compose one gigabyte.

ex:
2GB=?KB
2GB=2*1024MB=2048MB
2048MB=2048*1024KB=2097152KB

ex:
500KB=?GB
500KB=500/1024MB= 0.4883MB
0.4883MB=0.4883/1024GB=0.0004768GB

5
Types of Memory
A computer memory is of two types
1. Primary Memory (Internal storage)
2. Secondary Memory (External storage)

Primary Memory
Primary memory is also called internal memory and is an important part of a
computer. It is the main area in a computer where the data is stored. This
memory can be quickly accessed by the CPU for reading or storing information.
Primary memory is further classified into two types:
 Random Access Memory (RAM)
 Read - Only Memory (ROM )

Random Access Memory RAM


RAM is also known as read/write memory as information can be read from and
written onto it. RAM is a place in a computer that holds instructions for the
computer, its programs and the data. The CPU can directly access the data from
RAM almost immediately. However, the storage of data and instructions in RAM
is temporary, till the time the computer is running.

Read- Only Memory ROM


It is called Read-only memory as information can only be read from and not
written or changed onto ROM. ROM is the “built-in” memory of a computer. The
storage of data and instructions in ROM is permanent. It does not depend on
the power supply i.e. it is non-volatile memory.

Secondary Memory
The primary memory which is faster (and hence expensive) is generally not
sufficient for large storage of data. As a result, additional memory, called the
“auxiliary” or “secondary memory” is used. It is also referred as “backup
storage” as it is used to store large volume of data on a permanent basis which
can be transferred to the primary memory. Some of the devices of secondary
storages are Floppy Disk, Hard Disk, CD-ROM, DVD and Flash drive.

1. Floppy Disk
It is also referred as “Diskette”. The disks are available in two sizes of 5.25 and
3.5 inches. Storage capacity of floppies are measured in kilobytes (KB) and
megabytes (MB).

6
2. Hard Disk
The hard disk drive is the main, and usually largest, data storage hardware
device in a computer. Can be internal or external. A hard drive can be used to
store any data, including pictures, music, videos, text documents, and any files
created or downloaded. Also, hard drives store files for the operating system
and software programs that run on the computer. Older hard drives had a
storage size of several hundred megabytes (MB) to several gigabytes (GB).
Newer hard drives have a storage size of several hundred gigabytes to several
terabytes (TB).

3. CD-ROM
CD-ROM stands for Compact Disk–Read Only Memory. Its main advantage is that
it is portable and can hold a large amount of data. To use a CD-ROM, a device
called CD drive is needed.
CD-ROMs have the following variations:
(i) CD-R (Compact disc Recordable): Data can be written onto it just once. The
stored data can be read. Data once written onto it cannot be erased.
(ii) CD-RW (Compact disc Rewritable): It is also called erasable CD. Data once
written onto it can be erased to write or record new information many times.

4. DVD
DVD stands for Digital Versatile Disc. It is similar to a CD-ROM, except that it can
store larger amounts of data.
One of the most common DVD's is the single-sided, single-layer disc, capable of
holding 4.7 GB.
The single-sided, double-layer disc is capable of holding between 8.5-8.7 GB.
The double-sided, single-layer disc is capable of holding 9.4 GB.
Although rare, the double-sided, double-layer disc is capable of holding up to
17.08 GB.

5. Flash Drive
It is a small, portable device that can be used to store, access and transfer data.
They are available in sizes ranging from 2 GB to 1 TB.

7
HARDWARE AND SOFTWARE

Hardware
The physical components of the computer are known as “Hardware”. It refers to
the objects that we can actually touch.
Ex: input and output devices, processors, circuits and the cables.

Software
Software is a program or set of instructions that causes the Hardware to function
in a desired way.
There are five categories of software. They are:
1. Operating System
2. Translators
3. Utility programs
4. Application programs
5. General purpose programs

1. Operating System (OS)


The software that manages the resources of a computer system and schedules
its operation is called Operating system. The operating system acts as interface
between the hardware and the user programs and facilitates the execution of
programs.
The User interface provided by the OS can be character based or graphical.
CUI -- Character user Interface
GUI -- Graphical user Interface
CUI : It is operated with keyboard only. Ex: MS-DOS, UNIX
GUI : The system can be operated with mouse and keyboard. Ex: Windows 95,
Windows XP etc

2. Translators
Computers can understand instructions only when they are written in their own
language – the machine language . Therefore, a program written in any other
language should be translated into machine language. The software that
“translates” the instructions of different languages is known as translators .
There are two types of translators. They are compilers and Interpreters.

8
Programming Languages:
There are three types of programming languages.

a) Machine Languages: Computers respond only to machine language. This


language is in terms of binary codes (0,1). i.e. all programs should be written
with these codes, which is difficult.

b) Assembly Languages : It uses mnemonic codes rather than numeric codes (as
in machine languages). Mnemonic is an abbreviation for an operation. Ex. Add
or A is used as a symbol for addition, inc increase by one, BAL branch and link. It
requires translators to convert into machine language. Like machine language,
writing program in assembly language is also time consuming. These are also
machine dependent.

c) High Level Languages (HLL): These are referred as problem oriented languages
(POL). The high level languages are convenient for writing programs. Such
languages are considered high-level because they are closer to human
languages. These are referred as third generation languages.

Several High Level Languages which are in common use:


 FORTRAN
 COBOL
 BASIC
 C
 Pascal

3. Utility Programs
These are pre-written programs supplied by the manufacturer for maintaining
day to day activities of computer system.
Example: COPY, SORT, MAILING, virus scanning software, web browser, music
player

4. Application Programs:
These are user written programs to do a specific job which can be changed to
meet the individual needs. These programs are written in different languages
such as BASIC or C

5. General Purpose Packages


These packages are developed to suit the needs of research workers / scientists
in different fields. These packages are categorized as:

9
i) Data Analysis
ii) Word Processing
iii) Spread Sheet
iv) Graphics and
v) Databases

Data Analysis Ex: SPSS (Statistical Package for Social Science)


Word Processing Ex: MS-Word
Spread Sheet Ex: MS-Excel
Graphics Ex: POWER-POINT, Autocat
Databases Ex: MS-Access

Classification of Computers
Computers are classified according to the storage capacity, speed and the
purpose for which they are developed. These can be classified into three types:
1. Analog Computes
2. Digital Computers
3. Hybrid Computers

1. Analog computers: They operate by “measuring” instead of “counting”. They


are powerful tools for solving differential equations. An analog computer is a
form of computer that uses the continuously changeable aspects of physical
phenomena such as electrical, mechanical, or hydraulic quantities to model the
problem being solved.

2. Digital Computers: These computers operate by “counting”. All quantities are


expressed as discrete digits or numbers. These are useful for evaluating
arithmetic expressions and manipulations of data.

3. Hybrid Computers: Computers which combine the features of analog and


digital computers are known as Hybrid computers.

A majority of the computers that are in use are digital computers. The digital
computers are classified into
1) Special Purpose Computers and
2) General Purpose Computers

10
1. Special Purpose Computers: These are developed with a specific purpose.
Some of the areas where these computers are being used are – soil testing,
medical scanning, traffic signals, etc.,

2. General Purpose Computers: These are developed to meet the requirements


of several areas such as simulation, solving mathematical equations.
These computers are available in different sizes and capabilities and are further
classified (based on memory, speed, and storage) as follows:

Price, storage a) Super Computers Less widely used


capacity and
speed ↓
b) Mainframe Computers
c) Mini Computers ↕
d) Micro Computers More widely used

Super Computers: A supercomputer is the fastest, most powerful computer.


They have extremely large storage capacities and computing speeds which are
at least 10 times faster than other computers. These are used for large scale
numerical problems in scientific and engineering disciplines. The first super
computer was developed in U.S.A. by CRAY computers.

Mainframe Computers: A mainframe is a large, expensive, powerful computer


that can handle hundreds or thousands of connected users simultaneously. They
also have large storage and high computing speed (but relatively lower than the
super computers). They are used in applications like weather forecasting, space
applications etc., they support a large number of terminals for use by a variety
of users simultaneously, but are expensive.

Mini Computers: A minicomputer fills the space between the mainframe and
microcomputer, and is smaller than the former but larger than the latter.
Minicomputers are mainly used as small or mid-range servers operating
business and scientific applications.

Micro Computers: A micro computer is the smallest general purpose processing


system. Micro computers are also referred as “personal computers (PC)”.

11
CHAPTER 2
NUMBERING SYSTEMS and CONVERSION FROM ONE SYSTEM TO ANOTHER

The 4 number systems are:

Binary [ 2 ] .
 Uses 2 symbols: the digits 0 and 1.
 Some numbers in this system: (0)2, (000)2, (1010)2.

Decimal [ 10 ]
 Uses 10 symbols: the digits 0, 1, 2, … ,9.
 Some numbers in this system: (111)10, (0)10, (1010)10.

Octal [ 8 ]
 Uses 8 symbols: the digits 0, 1, 2, 3, 4, 5, 6, 7.
 Some numbers in this system: (111)8, (7)8, (1010)8.

Hexadecimal [ 16 ]
 Uses 16 symbols: 0, 1, 2, …., 9, A, B, C, D, E, F.
 Some numbers in this system: (111)16, (C)16, (1A10)16.

Conversion Between Numbering Systems


It is possible to convert between any of the number systems.

Conversion to Decimal System


1. Binary to Decimal Conversion
Method:
 Write down the binary number.
 List the powers of two from right to left.
 Multiply each digit with its corresponding power.
 Add the final values.

The method will be illustrated with the help of examples.

Ex: Convert (10110)2 into a decimal number.


1 0 1 1 0
4 3 2 1
2 2 2 2 20
1x24 0x23 1x22 1x21 0x20
Result= 1x24 + 0x23 + 1x22 + 1x21 + 0x20 =16 + 0 + 4 +2 + 0 = (22)10.

12
2. Octal to Decimal Conversion
Second step of the given method should be modified as
 List the powers of eight from right to left.

Ex: Convert (3462)8 into a decimal number.


3 4 6 2
83 82 81 80
3x83 4x82 6x81 2x80

Result= 3x83 + 4x82 + 6x81 + 2x80 = 1536 + 256 +48 + 2 = (1842)10.

3. Hexadecimal to Decimal Conversion


Second step of the given method should be modified as
 List the powers of 16 from right to left.

Ex: Convert (42AD)16 into a decimal number.


4 2 A D
83 82 81 80
4x163 2x162 Ax161 Dx160

note that A=10 and D=13


Result= 4x163 + 2x162 + 10x161 + 13x160
= 4x4096 + 2x256 +160 + 13 = (17069)10.

4. Conversion of Fractional numbers to Decimal System


Ex: Convert (1010.011)2 into a decimal number
1 0 1 0 . 0 1 1
3 2 1 0
2 2 2 2 . 2-1 2-2 2-3
1x23 0x22 1x21 0x20 . 0x2-1 1x2-2 1x2-3
Result= 1x23 + 0x22 + 1x21 + 0x20 + 0x2-1 + 1x2-2 + 1x2-3
=8 + 0 + 2 +0 + 0 + 0.25 + 0.125= (10.375)10.

Ex: Convert (362.35)8 into a decimal number


3 6 2 . 3 5
2 1 0
8 8 8 . 8-1 8-2
2 1 0
3x8 6x8 2x8 . 3x8-1 5x8-2
Result= 3x82 + 6x81 + 2x80 + 3x8-1 + 5x8-2

13
=192 + 48 + 2 + 0.375+ 0.078125= (242.453125)10.

Ex: Convert (42A.12)16 into a decimal number


4 2 A . 1 2
2 1 0
16 16 16 . 16-1 16-2
4x162 2x161 Ax160 . 1x16-1 2x16-2
Result= 4x162 + 2x161 + 10x160 + 1x16-1 + 2x16-2
=1024 + 32 + 10 + 0.0625+ 0.0078125= (1066.0703125)10.

Other conversions:
1. Decimal to Binary Conversion
To convert a number in decimal to a number in binary we have to divide
the decimal number by 2 repeatedly, until the quotient of zero is
obtained. This method of repeated division by 2 is called the ‘double-
dabble’ method. The remainders are noted down for each of the division
steps. Then the column of the remainder is read in reverse order i.e., from
bottom to top order.
ex: Convert (26)10 into a binary number.
Devision Quotient Generated remainder
26/2 13 0
13/2 6 1
6/2 3 0
3/2 1 1
1/2 0 1 Result: (11010)2

ex: Convert (523)10 into a binary number.


Devision Quotient Generated remainder
523/2 261 1
261/2 130 1
130/2 65 0
65/2 32 1
32/2 16 0
16/2 8 0
8/2 4 0
4/2 2 0
2/2 1 0
1/2 0 1 Result: (1000001011)2

14
ex: Convert (8023)10 into a binary number.
Devision Quotient Generated remainder
8023/2 4011 1
4011/2 2005 1
2005/2 1002 1
1002/2 501 0
501/2 250 1
250/2 125 0
125/2 62 1
62/2 31 0
31/2 15 1
15/2 7 1
7/2 3 1
3/2 1 1
1/2 0 1 Result: (1111101010111)2

2. Decimal to Octal Conversion


Same method but dive by 8

ex: Convert (426)10 into a octal number.


Devision Quotient Generated remainder
426/8 53 2
53/8 6 5
6/8 0 6 Result: (652)8

ex: Convert (1457)10 into a octal number.


Devision Quotient Generated remainder
1457/8 182 1
182/8 22 6
22/8 2 6
2/8 0 2 Result: (2661)8

3. Decimal to Hexadecimal Conversion


Same method but dive by 16

ex: Convert (348)10 into a hexadecimal number.


Devision Quotient Generated remainder
348/16 21 12 Note the this is equal to C
21/16 1 5
1/16 0 1 Result: (15C)16

15
ex: Convert (948)10 into a hexadecimal number.
Devision Quotient Generated remainder
948/16 59 4
59/16 3 11 note that this equal to B
3/16 0 3 Result: (3B4)16

4. Binary to Octal Conversion


We know that the maximum digit in an octal number system is 7, which
can be represented as (111)2 in a binary system. Hence, starting from the
Least Significant Bit LSB (far right), we group three (8=23) digits at a time
and replace them by the decimal equivalent of those groups and we get
the final octal number.

Ex: Convert (101101010)2 into an equivalent octal number.


Given number 101101010
3 bit groups 101 101 010
Decimal equivalent 5 5 2

Hence the result: (552)8.

Ex: Convert (1011110)2 into an equivalent octal number.


Given number 1011110
3 bit groups 1 (=001) 011 110
Decimal equivalent 1 3 6

Hence the result: (136)8.

5. Binary to Hexadecimal Conversion


maximum digit in a hexadecimal number system is 15 (which is F)
in binary system 15 can be represented as (1111)2
So, starting from the LSB, we group four digits (16=24) at a time

Ex: Convert (11010110)2 into an equivalent octal number.


Given number 11010110
4 bit groups 1101 0110
Decimal equivalent 13 (which is D) 6

Hence the result: (D6)16.

16
6. Octal to Binary Conversion
Each octal digit is converted into a 3-bit-equivalent binary number and
combining all those digits we get the final binary equivalent.

Ex: Convert (235)8 into an equivalent binary number.


Given number 235
Each digit 2 3 5
3 bit binary equivalent 010 011 101

Result: (010011101)2

7. Hexadecimal to Binary Conversion


Each hexadecimal digit is converted into a 4-bit-equivalent binary number
and combining all those digits we get the final binary equivalent.

Ex: Convert (29C)16 into an equivalent binary number.


Given number 29C
Each digit 2 9 C
4 bit binary equivalent 0010 1001 1100

Result: (001010011100)2

Conversion of Fractional Numbers


1. Decimal to Binary Conversion

ex: Convert (25.625)10 into a binary number.


Get the integer part, which is 25, first
Devision Quotient Generated remainder
25/2 12 1
12/2 6 0
6/2 3 0
3/2 1 1
1/2 0 1 in reverse order 11001

Now get the fractional part, 0.625


Step 1: Begin with the decimal fraction and multiply by 2 (since converting
to binary). The whole number part of the result is the first binary digit to
the right of the point.
0.625x2 1.25 1

17
Step 2: Next we disregard the whole number part of the previous result
(the 1 in this case) and multiply by 2 once again. The whole number part
of this new result is the second binary digit to the right of the point. We
will continue this process until we get a zero as our decimal part or until
we recognize an infinite repeating pattern.
0.25x2 0.50 0
0.50x2 1.00 1 Result : (11001.101)2

ex: Convert (7. 25)10 into a binary number.


Get the integer part, 7
Devision Quotient Generated remainder
7/2 3 1
3/2 1 1
1/2 0 1
Now get the fractional part, 0.25
0.25x2 0.50 0
0.50x2 1.00 1 Result: (111.01)2

2. Decimal to Octal Conversion

ex: Convert (8.125)10 into a octal number.


Devision Quotient Generated remainder
8/8 1 0
1/8 0 1 in reverse order 10

Now get the fractional part, 0.125


0.125x8 1.0 1 Result: (10.1)8

ex: Convert (62.0625)10 into a octal number.


integer part 62
Devision Quotient Generated remainder
62/8 7 6
6/8 0 6 in reverse order 66
fractiona part
0.0625x8 0.5024 0
0.5024x8 4.0192 4
0.0192x8 0.1536 0
0.1536x8 1.2288 1
0.2288x8 4.608 4 Result : (66.04014??)8

18
3. Decimal to Hexadecimal Conversion

ex: Convert (27.22)10 into a hexadecimal number.


integer part 27
Devision Quotient Generated remainder
27/16 1 11
1/16 0 1 in reverse order 1B

fractiona part, 0.22


0.22x16 3.52 3
0.52x16 8.32 8
0.32x16 5.12 5
0.12x16 1.92 1
0.92x16 14.72 14
0.72x16 11.52 11 Result : (1B.3851EB)16

ex: Convert (290.875)10 into a hexadecimal number.


integer part, 290
Devision Quotient Generated remainder
290/16 18 2
18/16 1 2
1/16 0 1 in reverse order 112

fractional part, 0.875


0.875x16 14.00 14 (E) Result : (112.E)16

4. Binary to Octal Conversion

Ex: Convert (1101.0111)2 into an equivalent octal number.


Grouping the fractional part: start from the right of the decimal point and
proceed toward the right

Given number 1101.0111


3 bit groups 001 101 . 011 100
Decimal equivalent 1 5 . 3 4

Result: (15.34)8.

19
5. Binary to Hexadecimal Conversion

Ex: Convert (10101.01011)2 into an equivalent hexadecimal number.


Grouping the fractional part: start from the right of the decimal point and
proceed toward the right
Given number 10101.01011
4 bit groups 0001 0101 . 0101 1000
Decimal equivalent 1 5 . 5 8

Result: (15.58)16.

Ex: Convert (10110101.01011)2 into an equivalent hexadecimal number.


Grouping the fractional part: start from the right of the decimal point and
proceed toward the right
Given number 10110101.01011
4 bit groups 1011 0101 . 0101 1000
Decimal equivalent 11 5 . 5 8

Result: (B5.58)16.

6. Octal to Binary Conversion


Ex: Convert (47.321)8 into an equivalent binary number.
Given number 47.321
Each digit 4 7 . 3 2 1
3 bit binary equivalent 100 111 . 011 010 001
Result: (100111.011010001)2

7. Hexadecimal to Binary Conversion


Ex: Convert (9E.AF2)16 into an equivalent binary number.
Given number 9E.AF2
Each digit 9 E . A F 2
4 bit binary equivalent 1001 1110 . 1010 1111 0010

Result: (10011110.101011110010)2

20
BINARY ARITHMETICS
Binary Addition
The four basic rules for adding binary digits (bits) are as follows:
0 + 0 = 0 Sum of 0 with a carry 0
0 + 1 = 1 Sum of 1 with a carry 0
1 + 0 = 1 Sum of 1 with a carry 0
1 + 1 = 1 0 Sum of 0 with a carry 1

examples:
110
+ 100
1010

111
+ 011
1010

1111
+ 1100
11011

1011
1111
+ 110
100000

Binary Subtraction
The four basic rules for subtracting are as follows:
0–0=0
1–1=0
1–0=1
0 – 1 = 1 0-1 with a borrow of 1

examples:
11
- 01
10

11
- 10

21
01

101
- 011
010

110
- 101
001

101101
- 001110
11111

22
CHAPTER 3
PROGRAMMING CONCEPTS

You needs a computer program (i.e. a set of instructions written in a computer


language) in order to be able to perform a specific task on a computer. Before
writing the computer program we write an algorithm to indicate the steps to be
followed in calculations or other problem-solving operations. In algorithms we
use our own daily language (English). Then we express the algorithm in graphical
form which is called a flowchart.

Way of problem solving using a computer program includes the following steps:
 Defining the Problem
State the problem you are trying to solve clearly and concisely (brief but
comprehensive).
 Writing the Algorithm
As it is already defined algorithm is the set of the steps related with the
method of solution of a given problem. This should be done in a
programming manner. This step also includes writing the followings:
 Pseudo-code
 Flowchart
 Writing the Program
Now the solution defined in the previous step is rewritten by using any
programming language (i.e. by obeying the rules of the used programming
language)
 Testing and Debugging the Program
The final step is running the written computer program on a computer
and fixing the errors if there is any.

Pseudo-code is an artificial and informal language that helps programmers


develop algorithms. Pseudocode is very similar to everyday language.

A flowchart is a schematic representation of an algorithm, showing the


sequence of the steps as boxes of various kinds, and their order by connecting
them with arrows. This diagrammatic representation illustrates a solution model
to a given problem.

23
The symbols used in a flow chart:

Name Symbol Meaning


Oval Denotes the beginning
and the end of the
program
Parallelogram Denotes input and output
operations

Rectangle Denotes a process to be


carried out, such as
mathematical operations
Diamond Denotes a decision to be
made. Such as logical
operations.
Hybrid Also used for an output
operation

Flow line Denotes the direction of


logic flow in the program

Example
The final grade is calculated as the average of four marks. Student fails if final
grade is less than 50. Write an algorithm to determine a student’s final grade
and indicate whether it is passing or failing.

Algorithm
 Input a set of 4 marks
 Get the final grade by adding marks and dividing it by 4.
 If the grade is below 50
then write “FAIL”
otherwise write “PASS”

24
Pseudocode
Step 1: START
Step 2: Input M1, M2, M3, M4
Step 3: GRADE ← (M1 + M2 + M3 + M4) / 4
Step 4: IF ( GRADE < 50 ) THEN
Print “FAIL”
ELSE
Print “PASS”
ENDIF
Step 5: END

Flowchart

25
Example
Write an algorithm that read two values, determines the largest values and
prints the largest values with an identifying massage.

Algorithm
 Input two numbers
 Compare the numbers and find the maximum
 Print the message “the largest value is:” and value of MAX

Pseudocode
Step 1: START
Step 2: INPUT NUMBER1, NUMBER2
Step 3: IF NUMBER1 > NUMBER 2 THEN
MAX←NUMBER1
ELSE
MAX←NUMBER2
ENDIF
Step 4: PRINT “The largest value is:” , MAX
Step 5: END

Flowchart

26
DECISION STRUCTURES
Decision structures can test conditions and then depending on the results of that
test, perform different operations. There are more than one decision structure
such as:
 IF structure
 IF THEN ELSE structure

IF structure is as follows:
IF condition THEN
Here you may have one or more comments to the computer.
These comments will be executed if the condition is true.
Otherwise control passes directly to the next step after ENDIF.
ENDIF

Here condition is nothing but a logical operation such as A > B whose result is
either true or false.

IF THEN ELSE structure is as follows:


IF condition THEN
Here you may have one or more comments to the computer.
These comments will be executed if the condition is true.
Afterwards the control passes to the next step after ENDIF
IF the condition is false, control passes directly to the statements
written in the else block.
ELSE
Here you may have one or more comments to the computer.
These comments will be executed if the condition is false.
Afterwards the control passes to the next step after ENDIF.
ENDIF

27
Example: Write an algorithm/pseudo code that reads three numbers and prints
the value of the largest one. Also draw the flowchart.

Step 1: Start
Step 2: Input N1, N2, N3
Step 3: IF N1 > N2 THEN
IF N1 > N3 THEN
MAX ← N1
ELSE
MAX ← N3
ENDIF
ELSE
IF N2 > N3 THEN
MAX ← N2
ELSE
MAX ← N3
ENDIF
ENDIF
Step 4: Print “The largest number is”, MAX
Step 5: End

The IF structure given in the above example is known as NESTED IF. i.e. in IF
THEN block and/or in ELSE block involve another decision structure.

DRAW FLOWCHART

28
LOOPS
In computer programming, a loop is a sequence of instructions that is continually
repeated until a certain condition is reached.
Depending on the programming language used, different types of loops may be
available:
 While loops
 Repeat until loops
 Do loops
 For loops

While Loop
While loop has the following structure:
WHILE condition
The comments written between WHILE condition and END WHILE
will be executed if the condition is true. Then the condition will be
checked again and the block will be repeated once more. And this
will go on. If the condition is false the control passes to the next
step after the END WHILE without executing the while block.
END WHILE

Here condition is nothing but a logical operation such as A > B whose result is
either true or false.

Example: Write an algorithm/pseudo code and draw the flowchart to calculate


212. Use a loop for the calculation.
Note: 212 = 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x2 x 2

29
Step 1: Start
Step 2: Base ← 2
Step 3: Power ← 12
Step 4: Product ← Base
Step 5: Counter ← 1
Step 6: WHILE Counter < Power
Step 7: Product ← Product *Base
Step 8: Counter ← Counter +1
END WHILE
Step 9: Print Product
Step 10: End

TRACING an ALGORITM/PSEUDO-CODE/COMPUTER PROGRAM


Tracing is a method for hand simulating the execution of a code in order to
manually verify that it works correctly before you compile it.

30
Example: Trace the above code for power = 4.
Base Power Product Counter Counter < Power
Step2 2 ? ? ? ?
Step3 2 4 ? ?
Step4 2 4 2 ?
Step5 2 4 2 1
Step6 2 4 2 1 1 < 4, True
Step7 2 4 2*2=4 1
Step8 2 4 4 1+1=2
Step6 2 4 4 2 2 < 4, True
Step7 2 4 4*2=8 2
Step8 2 4 8 2+1=3
Step6 2 4 8 3 3 < 4, True
Step7 2 4 8*2=16 3
Step8 2 4 16 3+1=4
Step6 2 4 16 3 4 < 4, False
Step9 Print 16

Base=2
Power=4
Product=2
Counter=1
Is Counter < Power (1<4) Yes 16
Product=2*2 (4)
Counter=1+1 (=2)
Is Counter < Power (2<4) Yes
Product=4*2 (=8)
Counter=2+1 (=3)
Is Counter < Power (3<4) Yes
Product=8*2 (=16)
Counter=3+1 (=4)
Is Counter < Power (4<4) NO
computer will print 16

31
Example: Write an algorithm and draw a flowchart in order to find and print the
following summation: sum= 1 + 2 + 3+ ….+ 100

Step 1: Start
Step 2: Sum ← 0
Step 3: Counter ← 1
Step 4: WHILE Counter <= 100
Step 5: Sum ← Sum + Counter
Step 6: Counter ← Counter +1
END WHILE
Step 7: Print Sum
Step 8: End

Example: Write an algorithm and draw a flowchart in order to find and print the
following summation:
sum= 3+ 5 + 7….+ 15

Step 1: Start
Step 2: Sum ← 0
Step 3: Counter ← 3
Step 4: WHILE Counter <= 15
Step 5: Sum ← Sum + Counter
Step 6: Counter ← Counter +2
END WHILE
Step 7: Print Sum
Step 8: End

Example: Write an algorithm and draw a flowchart in order to find and print the
following summation:
sum= 10+ 12 + 14….+ 56

Step 1: Start
Step 2: Sum ← 0
Step 3: Counter ← 10
Step 4: WHILE Counter <= 56
Step 5: Sum ← Sum + Counter
Step 6: Counter ← Counter +2
END WHILE
Step 7: Print Sum
Step 8: End

32
Example: Write an algorithm, draw a flowchart and trace the algorithm.
Algorithm will request the user to enter 5 number from the keyboard and print
the summation of the entered numbers. The program will check the number of
elements and doesn’t allow to enter more or less than 5 numbers.

Step 1: Start Step 1: Start


Step 2: Sum ← 0 Step 2: Sum ← 0
Step 3: Count ← 1 Step 3: Count ← 1
Step 4: Print “Enter a number” Step 4: WHILE Count <= 5
Step 5: Enter N Step 5: Print “Enter a number”
Step 6: Sum ← Sum + N Step 6: Enter N
Step 7: Counter ← Counter + 1 Step 7: Sum ← Sum + N
Step 8: IF Counter <= 5 THEN Step 7: Counter ← Counter + 1
Step 9 Goto Step 4 ENDWHILE
ENDIF Step 8: Print Sum
Step 10: Print Sum Step 9: End
Step 11: End

Example: Write an algorithm which requests the user to enter an inter.


Algorithm will check if the input number is negative or not. If it is negative then
will print 6 more of the input number, otherwise it will print the twice of the
input number.

Step 1: Start
Step 2: Print “Please enter an integer number”
Step 3: Input N
Step 4: IF N < 0 THEN
Step 5: Result ← N + 6
ELSE
Step 6: Result ← N * 2
ENDIF
Step 7: Print Result
Step 8: End

Example: Write an algorithm and draw a flowchart in order to find and print the
following summation:
sum= 13 + 33 + 63+ ….+ (2N-1)3 = ∑ 2 −1
where N is an integer which is going to be entered by the user.

33
Step 1: Start
Step 2: Print “Enter a positive integer number”
Step 3: Enter N
Step 4: Sum ← 0
Step 5: Counter ← 1
Step 6: WHILE Counter <= N
Step 7: Sum ← Sum + (2*Counter - 1)^3
Step 8: Counter ← Counter +1
END WHILE
Step 9: Print Sum
Step 10: End

OR you can construct the loop by using IF statement

Step 1: Start
Step 2: Print “Enter a positive integer number”
Step 3: Enter N
Step 4: Sum ← 0
Step 5: Counter ← 1
Step 6: IF Counter <= N THEN
Step 7: Sum ← Sum + (2*Counter - 1)^3
Step 8: Counter ← Counter +1
Step 9: GOTO Step 6
END WHILE
Step 9: Print Sum
Step 10: End

NOTE: by using GOTO statement you can directly access any desired step in the
code. Be careful, by using GOTO statement you can leave the Decision Structure
or the Loop Structure but you cannot directly access to any IF Then Block or Else
Block or While Block from outside the block.

Example: Write an algorithm and draw a flowchart in order to find and print the
following summation:
sum= 25 + 48 + 611+ ….+ (2N)3N+2 = ∑ 2
where N is an integer which is going to be entered by the user. If the user enters
a negative integer ask him/her to enter an integer number.

34
Step 1: Start
Step 2: Print “Enter a positive integer number”
Step 3: Enter N
Step 4: IF N < 0 THEN
Step 5: GOTO Step 2
ENDIF
Step 6: Sum ← 0
Step 7: Counter ← 1
Step 8: WHILE Counter <= N
Step 9: Sum ← Sum + (2*Counter)^(3*counter+2)
Step 10: Counter ← Counter + 1
END WHILE
Step 11: Print Sum
Step 12: End

TRACE THE ALGORITHM FOR N=4


N Sum Counter N < 0 Counter <= N
Step2 ? ? ? ?
Step3 4 ? ?
Step4 4 ? ? 4 < 0, False
Step6 4 0 ?
Step7 4 0 1
Step8 4 0 1 1 <= 4, True
Step9 4 =0 +(2*1)^(3*1+2) 1
=32
Step10 4 =32 =1+1=2
Step8 4 32 2 2 <= 4, True
Step9 4 =32+(2*2)^(3*2+2) 2
=65568
Step10 4 65568 =2+1=3
Step8 4 65568 3 3 < 4, True
Step9 4 =65568+(2*3)^(3*3+2) 3
=362862624
Step10 4 =362862624 =3+1=4
Step8 4 362862624 4 4 <= 4, True
Step9 4 =362862624+(2*4)^(3*4+2) 4
=4398409373728
Step10 4 4398409373728 5
Step8 4 4398409373728 5 5 <= 4, False
Step11 Print 4398409373728

35
36

You might also like