0% found this document useful (0 votes)
30 views170 pages

Unit 1 - Fundamentals of Computing

The document provides an overview of a course on programming for problem solving using C. The course objectives are to learn the fundamental components and operating principles of computers and to find solutions to complex engineering problems by developing computer programs using C. The course outcomes include the ability to understand number systems, construct algorithms and flowcharts, apply C programming concepts, and develop programs using arrays, strings, functions, and pointers in C for engineering applications.

Uploaded by

Preetha V
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views170 pages

Unit 1 - Fundamentals of Computing

The document provides an overview of a course on programming for problem solving using C. The course objectives are to learn the fundamental components and operating principles of computers and to find solutions to complex engineering problems by developing computer programs using C. The course outcomes include the ability to understand number systems, construct algorithms and flowcharts, apply C programming concepts, and develop programs using arrays, strings, functions, and pointers in C for engineering applications.

Uploaded by

Preetha V
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 170

20ITG01-PROGRAMMING

FOR PROBLEM SOLVING


USING C

PREETHA V AP/CSE, SRIT


Course Objectives

To learn the fundamental components and operating principles of


digital computer

To find solutions to complex engineering problems by developing


computer programs using C

PREETHA V AP/CSE, SRIT


Course Outcomes
CO1: Ability to understand the concept of the number systems and to transform
from one form to another form.
CO2: Ability to construct algorithms, Pseudo codes and flow charts for real world
problems.
CO3: Ability to apply the concepts of C programming.CO4: Ability to develop
program using arrays and strings in C language.
CO4: Ability to develop and analyze computer programs in C for Engineering
Applications.

PREETHA V AP/CSE, SRIT


Fundamentals of Computing
Basic concepts of computer organizations, Generation and
classification of computers, Number System Representation,
Fundamentals of algorithms, Pseudo code, Flow charts.
C Language Fundamentals
Introduction to C programming, Structure of a C program,
Compilation and Linking Processes, Character Set, Identifiers,
Keywords, Data Types, Constant and Variables, Statements,
Expressions, Operators, Precedence of operators, Input-Output
Operations, Control Structures, Decision Making, Branching &
Looping. Application: Solving Simple Scientific and
Mathematical Problems.

PREETHA V AP/CSE, SRIT


Arrays and Strings
Introduction to Arrays, One Dimensional Array, Multidimensional Array. Application:
Matrix Operations, Sorting, Searching, Sum of Series and Statistical Problems. String
Manipulation, String Arrays. Application: Solving problems using String Functions.
Functions and Pointers
User Defined and Standard Functions, Formal and Actual arguments, Function
Prototypes, Parameter Passing, Call-by-Value, Call-by-Reference, Recursion.
Application: Math Functions, Computation of Sine Series, Random Number
Generation, Tower of Hanoi and Factorial using Recursive Functions. Pointers, Pointer
Variables, Pointer Arithmetic, Passing Parameters by Reference, Pointer to Pointer,
Pointers to Functions, Dynamic Memory Allocation. Application: Card shuffling and
Dealing Simulation using Pointers.
Structures, Unions and File Handling
Declaration of Structures, Nested Structure, Pointer to Structure, Declaration of Unions,
Pointer to Union, Application: Student Records. Storage Classes, Pre-Processor
Directives. Files -Types of File Processing: Sequential Access, Random Access.
Application: Transaction Processing Program.

PREETHA V AP/CSE, SRIT


FUNDAMENTALS OF
COMPUTING
Unit 1

PREETHA V AP/CSE, SRIT


What’s a Computer?

A computer is an electronic
machine that takes input from the
user, processes the given input and
generates output in the form of
useful information.

PREETHA V AP/CSE, SRIT


 The Input to the computer may be any 1 of the
following:
 Data
 Programs
 User reply

 Data: the raw details that need to be processed to


generate some useful information.
 Programs: the set of instructions that can be executed
by the computer in sequential or non-sequential
manner.
 User reply: the input provided by the user in response
to a question asked by the computer.
PREETHA V AP/CSE, SRIT
PREETHA V AP/CSE, SRIT
Basic concepts of
computer
organization

PREETHA V AP/CSE, SRIT


Basic Computer Organization

 The basic computer organisation involves the


interfacing of different units of the computer and
various operations performed between these
units.

 The basic computer organisation explains the way


in which different units of computer are
interconnected with each other and controlled.
Some of the basic units of computer organisation
are:
• Input Unit
• Memory Unit
• CPU
• Output Unit
PREETHA V AP/CSE, SRIT
Input unit

 The most commonly used input devices are:


 Keyboard
 Mouse
 Scanner

Keyboard:
 A standard keyboard includes alphanumeric keys, function keys,
modifier keys, cursor movement keys, spacebar, escape key,
numeric keypad, and some special keys, such as Page Up, Page
Down, Home, Insert, Delete and End.
PREETHA V AP/CSE, SRIT
Mouse:

 The mouse allows the user to select elements on the screen, such
as tools, icons, and buttons, by pointing and clicking them.
 The mouse is also known as a pointing device because it helps
change the position of the pointer or cursor on the screen.
 The mouse consists of two buttons, a wheel at the top and a ball
at the bottom of the mouse.

Scanner:

 A scanner is an input device that converts documents and images


as the digitized images which is understandable by the computer
system.
 The digitized images can be produced as black and white images,
gray images, or colored images.
PREETHA V AP/CSE, SRIT
Memory Unit

 The memory unit of a computer is used to store data, instructions


for processing data, intermediate results of processing and the
final processed information.

 There are two types of memory:

 Primary memory
 Secondary memory

PREETHA V AP/CSE, SRIT


 Primary memory: Commonly used primary memories are:
 ROM
 RAM
 Cache memory

 Secondary memory: Commonly used secondary storage


devices are:
 Magnetic storage disk
 Optical storage disk
 Magneto-optical storage disk
 Universal Serial Bus (USB) drive

PREETHA V AP/CSE, SRIT


Primary Memory
 The primary memory is available in the computer as a built-in unit
of the computer. The primary memory is represented as a set of
locations with each location occupying 8 bits.

 ROM, represents Read Only Memory that stores data and


instructions, even when the computer is turned off.
 It is the permanent memory of the computer where the contents
cannot be modified by an end user.

 RAM (Random Access Memory) is the read/write memory unit in


which the information is retained only as long as there is a regular
power supply.
 When the power supply is interrupted or switched off, the information
stored in the RAM is lost. RAM is a volatile memory that temporarily
stores data and applications as long as they are in use.
 When the use of data or the application is over, the content in RAM is
erased.
PREETHA V AP/CSE, SRIT
 Some of the key differences between RAM and ROM:
RAM ROM
1. It is a read/write It is a read only memory
memory
2. It is volatile storage It is a permanent storage
device. device.
3. Data is erased as Data remains stored even
soon as power supply after power supply has been
is turned off. turned off
4. It is used as the main It is used to store Basic
memory of a computer input output system (BIOS).

PREETHA V AP/CSE, SRIT


Cache memory:

 Cache memory is used to store the data and the related application that
was last processed by the CPU.
 When the processor performs processing, it first searches the cache
memory and then the RAM, for an instruction.
 The cache memory is always placed between CPU and the main memory
of the computer system.

PREETHA V AP/CSE, SRIT


Secondary Memory

 Secondary memory represents the external


storage devices that are connected to the
computer.
 They provide a non-volatile memory source
used to store information that is not in use
currently. The secondary storage devices can
be classified as:

Magnetic storage device:


 The magnetic storage devices store information
that can be read, erased and rewritten a number
of times. These include floppy disk, hard disk and
magnetic tapes.
PREETHA V AP/CSE, SRIT
Optical storage device:
 The optical storage devices are secondary storage devices that use
laser beams to read the stored data.
 These include CD-ROM, rewritable compact disk (CD-RW), and
digital video disks with read only memory (DVD-ROM).

Magneto-optical storage device:


 The magneto-optical devices are generally used to store
information, such as large programs, files and backup data.
 The end user can modify the information stored in magneto-
optical storage devices multiple times.
 These devices provide higher storage capacity as they use laser
beams and magnets for reading and writing data to the device.
Examples of magneto-optical devices include Sony MiniDisc,
Maxoptix T5-2600, etc.
PREETHA V AP/CSE, SRIT
Universal serial bus (USB) drive:

 USB drive or commonly known as pen drive is a removable storage


device that is interfaced on the USB port of a computer system.
 It is pretty fast and compact in comparison to other storage devices
like CD and floppy disk.
 One of the most important advantages of a USB drive is that it is
larger in capacity as compared to other removable storage devices.
Off late, it has become very popular amongst computer users .

PREETHA V AP/CSE, SRIT


CPU

 The function of any computer system revolves around a central


component known as CPU.

 The CPU, which is popularly referred as the “brain” of the computer,


is responsible for processing the data inside the computer system.

 It is also responsible for controlling all other components of the


system.

PREETHA V AP/CSE, SRIT


CPU

 The main operations of the CPU include four


phases:
 Fetching instructions from the memory
 Decoding the instructions to decide what
operations are to be performed
 Executing the instructions
 Storing the results back in the memory

 The three main components of CPU are:


 Arithmetic and Logic Unit (ALU)
 Control Unit (CU)
 Registers

PREETHA V AP/CSE, SRIT


Arithmetic and Logic Unit

 ALU is a part of the CPU that performs arithmetic and logical operations
on the data.

 The arithmetic operations can be addition, subtraction, multiplication or


division.

 The multiplication and division operations are usually implemented by the


ALU as the repetitive process of addition and subtraction operations
respectively.

 It takes input in the form of an instruction that contains an opcode,


operands and the format code.
 Opcode: specifies the operation to be performed
 Operands: specifies the data on which the operation is to be
performed.
 Format code: suggests the format of the operands, such as, fixed point
or floating point.
PREETHA V AP/CSE, SRIT
Arithmetic and Logic Unit
(cont…)
 The output of ALU contains the result of the
operation and the status of the result, whether it
is final or not. The output is stored in a storage
register by the ALU.

 It also performs 16 different types of logical


operations. The various logical operations include
greater than (>), less than (<), equal to (=), not
equal to (π), shift left, shift right, etc. It makes
use of various logic gates, such as AND, OR, NOR,
etc. for performing the logical operations on the
data.
PREETHA V AP/CSE, SRIT
Control Unit (CU)

 Control Unit is an important component of CPU that


controls the flow of data and information.

 It maintains the sequence of operations being


performed by the CPU.

 It fetches an instruction from the storage area, decodes


the instruction and transmits the corresponding signals
to the ALU and the storage registers.

PREETHA V AP/CSE, SRIT


Control Unit (cont..)
 CU guides the ALU about the operations that are to
be performed and also suggests the I/O devices to
which the data is to be communicated.

 CU uses a program counter register for retrieving


the next instruction that is to be executed.

 It also uses a status register for handling conditions


such as overflow of data.

PREETHA V AP/CSE, SRIT


Registers
 Central processing unit contains a few, special-purpose, temporary
storage units known as registers.

 They are high-speed memory locations used for holding


instructions, data and intermediate results that are currently
being processed.

 Many computers employ additional registers for implementing


various other requirements.

 The number and sizes of registers, therefore, vary from processor


to processor.
PREETHA V AP/CSE, SRIT
Registers (cont…)

 A processor can have different types of registers to hold


different types of information, they are:

 Program Counter (PC) to keep track of the next instruction to be


executed.
 Instruction Register (IR) to hold instructions to be decoded by the
control unit.
 Memory Address Register (MAR) to hold the address of the next
location in the memory to be accessed.
 Memory Buffer Register (MBR) for storing data received from or
sent to CPU.
 Memory Data Register (MDR) for storing operands and data.
 Accumulator (ACC) for storing the results produced by arithmetic
and logic units.
PREETHA V AP/CSE, SRIT
Output Unit

 Output unit is an electronic device, which is used


to communicate the output obtained after
processing a specific task, to the user.

 The data processed by the CPU, is made available


to the end user by the output devices. The most
commonly used output devices are:
 Monitor
 Printer
 Speaker
PREETHA V AP/CSE, SRIT
Monitor
 A monitor is the most commonly used output device that produces
visual displays generated by the computer.

 The display device is used for visual presentation of textual and


graphical information.

 The monitors can be classified as


 Cathode Ray Tube (CRT) monitors or
 Liquid Crystal Display (LCD) monitors.

 The CRT monitors are large, occupy more space in the computer,
whereas LCD monitors are thin, light weighted, and occupy lesser
space.
PREETHA V AP/CSE, SRIT
Monitor (cont..)
 Both the monitors are available as monochrome, gray scale and
color models. However, the quality of the visual display produced
by the CRT is better than that produced by the LCD.
 A monitor can be characterized by the following two factors,
 monitor size and
 resolution.
 The monitor size is the length of the screen that is measured
diagonally.
 The resolution of the screen is expressed as the number of picture
elements or pixels of the screen. The resolution of the monitor is
also called the dot pitch.
 The monitor with a higher resolution produces a clearer image.

PREETHA V AP/CSE, SRIT


Printer

 The printer is an output device that transfers the text displayed on


the screen, onto paper sheets that can be used by the end user.

 The various types of printers used in the market are generally


categorized as
 dot matrix printers
 inkjet printers
 laser printers.

 Dot matrix printers are commonly used in low quality and high
volume applications like invoice printing, cash registers, etc.

 However, inkjet printers are slower than dot matrix printers and
can generate high-quality photographic prints.
PREETHA V AP/CSE, SRIT
Printer (cont..)

 Since, laser printers consist of microprocessor, ROM and RAM,


they can produce high-quality prints in quicker time without
being connected to a computer.

 The performance of a printer is measured in terms of dots per


inch (DPI) and pages per minute (PPM) produced by the printer.

 The greater the DPI parameter of a printer, the better is the


quality of the output generated by it.

 The higher PPM represents higher efficiency of the printer.

PREETHA V AP/CSE, SRIT


Speaker
 The speaker is an electromechanical transducer that converts an
electrical signal into sound.

 They are attached to a computer as output devices, to provide


audio output, such as warning sounds and Internet audios.

 We can have built-in speakers or attached speakers in a


computer to warn end users with error audio messages and
alerts.

 The sound card being used in the computer system decides the
quality of audio that we listen using music CDs or over the
Internet.

 The computer speakers vary widely in terms of quality and price.


PREETHA V AP/CSE, SRIT
Generation and
Classification of
Computers

PREETHA V AP/CSE, SRIT


GENERATIONS OF COMPUTERS

First Generation 1940-1956-Vaccum Tubes


 Used vaccum tubes for circuitry
 Magnetic drums for memory
 Enormous in size almost takes up entire room
 Very expensive
 Consumed great deal of electricity
 Generated lot of heat
 Relied on machine language

PREETHA V AP/CSE, SRIT


Vaccum tubes

PREETHA V AP/CSE, SRIT


Magnetic Drums

PREETHA V AP/CSE, SRIT


PREETHA V AP/CSE, SRIT
Contd.,

 Could solve only one problem at a time

 Inputs based on punched cards.

 Outputs displayed on printouts.

 Examples: UNIVAC and ENIAC

PREETHA V AP/CSE, SRIT


Punched cards

PREETHA V AP/CSE, SRIT


Second Generation- 1956-1963-Transistors:
 Vaccum tubes replaced by transistors
 Computers became smaller, faster, cheaper, more energy-efficient and more reliable.
 Input were punched cards
 Output were printouts
 Moved from machine language to assembly language
 COBOL and FORTRAN were developed
 Memory moved from magnetic drum to magnetic core technology

PREETHA V AP/CSE, SRIT


Transistors

PREETHA V AP/CSE, SRIT


PREETHA V AP/CSE, SRIT
Third Generation- 1964-1971-Integrated Circuits
 Integrated circuits were developed
 Transistors were miniaturized and placed on silicon chips called semiconductors.
 Increased speed and efficiency of computers
 Inputs were keyboards and monitors
 Cheaper and smaller

PREETHA V AP/CSE, SRIT


Integrated Circuits

PREETHA V AP/CSE, SRIT


PREETHA V AP/CSE, SRIT
Fourth Generation- 1971-Present-Microprocessor
 Thousands of integrated circuits built onto a single silicon chip
 Intel 4004 chip developed in 1971 located all components of computer
 1981-IBM introduced first computer for home user
 1984-APPLE introduced Macintosh
 Development of networks, GUI’s, mouse and Handheld devices

PREETHA V AP/CSE, SRIT


Microprocessor

PREETHA V AP/CSE, SRIT


PREETHA V AP/CSE, SRIT
Fifth Generation- Present and Beyond-Artificial Intelligence
 Based on AI, still in development
 Some applications are voice recognition
 Use of parallel processing and super conductors help in making AI a reality
 Goal is to develop devices that respond to natural language input

PREETHA V AP/CSE, SRIT


PREETHA V AP/CSE, SRIT
CLASSIFICATION OF
COMPUTERS
 Classified according to the following
three criteria:

1. Based on operating principles


2. Based on applications
3. Based on size and capability

PREETHA V AP/CSE, SRIT


Based on operating principles

 Based on operations performed and methods


used to store and process data and information,
computers can be classified into the following
categories:

• Analog computers
• Digital computers
• Hybrid computers
PREETHA V AP/CSE, SRIT
Analog computers:

 Represent data in the form of continuous electrical signals


 Example: Thermometer, Speedometer

Digital computers:

 Stores and processes data in the digital form


 Data is stored in terms of 0s and 1s.
 Output produced by these computers is also in the digital form.
 Faster and more reliable than analog computers
 Provide accurate results
 Example: Desktops and Laptops
PREETHA V AP/CSE, SRIT
Hybrid computers:

 Combination of analog and digital computer


 Therefore, the hardware components of hybrid computers are
usually the mixture of analog and digital components.
 Fast, efficient and reliable.
 Data is measured and processed in the form of electrical signals
and is stored with the help of digital components.
 The hybrid computers are also less expensive than the digital
computers.
 Examples: computers used in hospitals to measure the heartbeat
of a patient

PREETHA V AP/CSE, SRIT


PREETHA V AP/CSE, SRIT
Based on Applications
On the basis of different applications or purposes, computers can be
classified into the following categories:
General-purpose computers:
 Designed to work in all environments.
 Versatile in nature.
 However, they consume a large amount of time in generating the
result.

Special-purpose computers:
 Perform only a specified task.
 Not versatile
 Speed and memory size depend on the task that is to be performed.
 Efficient and consume less amount of time in generating the result.
PREETHA V AP/CSE, SRIT
Based on Size and Capability

 On the basis of size and capability, computers can


be classified into the following categories:

• Microcomputers
• Mini computers
• Mainframe computers
• Super computers

PREETHA V AP/CSE, SRIT


Microcomputers:
 Small and cheap digital computer, designed to be used by individuals.
 Built around a microprocessor, a storage unit and an I/O channel.
 Generally available in the form of PCs, workstations and notebook
computers

 Depending on the size, the microcomputer can be further classified into the
following types:
 Desktop computer
 Laptop computer
 Hand-held Computer
 Notebook
 Tablet
 Smart Phones

PREETHA V AP/CSE, SRIT


Desktop Computers:

 Most common type of microcomputers

 Contains 3 units-keyboard, monitor and a system containing


CPU, Memory, Hard disk, etc

 In expensive

 Suited for a single user at home, small business units

 Example: Apple, Microsoft, HP, Dell and Lenovo


PREETHA V AP/CSE, SRIT
Laptop Computers:

 Portable

 Have all features of a desktop computer

 Costlier then desktop computers

 Advantages:
 Small in size
 Can be carried anywhere

PREETHA V AP/CSE, SRIT


Notebook:

 Smaller notebooks optimized for low weight and low cost

 Designed for accessing web based application

 Notebooks deliver the performance needed to enjoy


popular activities like streaming videos or music,
emailing, web surfing or instant messaging

PREETHA V AP/CSE, SRIT


Tablet:

 Has features of notebook computers but it accepts input from a stylus instead of
keyboard or mouse

 Portable

 New kind of PC’s

PREETHA V AP/CSE, SRIT


Handheld/Personal Digital Assistance(PDA):
 Small computers can be held on the top of palm

 Small in size

 Uses stylus/pen for input, instead of keyboard

 Used for computing and information storage for presonal or business use

 Often for keeping schedules, calendars and address book handy

 Limited memory and less powerful

 Can be connected to internet via a wireless connection

 Example: Casio and Apple


PREETHA V AP/CSE, SRIT
PDA Contd.,

PREETHA V AP/CSE, SRIT


Smart Phones:
 Cellular phones that function both as phone and as a small PC
 may use stylus or small keyboard
 Connected to internet wirelessly
 Used to access electronic mail, download music, play games, etc
 Examples: Blackberry, Apple, HTC, LG

PREETHA V AP/CSE, SRIT


Mini Computers:

 First introduced in 1960 by Digital Equipment Corporation (DEC).

 They were called mini computers because of their smaller size than the other
computers of those times.

 Can handle more data and more input and output than micro computers.

 Less powerful than mainframe computers but more powerful than micro
computers.

 Therefore, they are also referred to as the midrange computers.

PREETHA V AP/CSE, SRIT


Super computers:

 Fastest type of computer that can perform complex operations


at a very high speed.

 Expensive than the other categories of computers

 Specially designed for the applications in which large number


of complex calculations have to be carried out to get the
desired output.

 Reason behind the fastness is they are designed to execute


small number of programs at a time rather than many
programs simultaneously.

PREETHA V AP/CSE, SRIT


Contd.,
 Multiprocessing and multiprogramming techniques are used to solve complex
problems
 Multiprocessing :
Multiprocessing is a system that has two or more than one processors. In this, CPUs
are added for increasing computing speed of the system.
 Multi-programming :
Multi-programming is more than one process running at a time, it increases CPU
utilization by organizing jobs (code and data) so that the CPU always has one to
execute.
 Since it performs the arithmetic operations at a high speed, it is used for weather
forecasting
 Example: IBM, Silicon, Fujitsu and Intel.

PREETHA V AP/CSE, SRIT


Mainframe computers:

 Very large computer that is employed by large business organizations


for handling major applications, such as financial transaction
processing, industry and consumer statistics, and census.
 Capable of handling almost millions of records in a day.
 Actually considered as the predecessor of servers.
 Bigger and more expensive than other computers.

An IBM System Z9 mainframe. Inside of an IBM System Z9 mainframe


PREETHA V AP/CSE, SRIT
Contd.,
 ATM Transactions support large number of terminals for simultaneous user by a
number of user.
 Example: IBM 370, S/390

PREETHA V AP/CSE, SRIT


Number System
Representation

PREETHA V AP/CSE, SRIT


Number System

 Set of values used to represent different quantities is known as Number System".

 For example, a number system can be used to represent the number of students in a
class.

PREETHA V AP/CSE, SRIT


 The Decimal Number System consists of ten digits from 0 to 9.
 These digits can be used to represent any numeric value.

 The base of decimal number system is 10.

 Each number in this system consists of digits which are located at different
positions.

 The position of first digit towards left side of the decimal point is 0.

PREETHA V AP/CSE, SRIT


 The position of second digit towards left side of the decimal point is 1.

 Similarly, the position of first digit towards right side of decimal point is -1.

 The position of second digit towards right side of decimal point is -2 and so on.

PREETHA V AP/CSE, SRIT


Number System

 Number Base B => B symbols


 Base 16(Hexa):0, 1,……9, A ,…, E,
F
 Base 10 (Decimal): 0, 1, 2,……, 7, 8,
9
 Base 8(Octal): 0, 1, 2, 3, 4, 5, 6, 7
 Base 2 (Binary): 0, 1

PREETHA V AP/CSE, SRIT


Decimal Numbers: Base 10

 Base or Radix is 10
 Digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

 Example:

41210 = 4x102 + 1x101 + 2x100


= 400 + 10 +2 = 412

PREETHA V AP/CSE, SRIT


Binary Numbers: Base 2

 Base or Radix is 2

 Digits: 0, 1

 Example:

1012 = 1x22 + 0x21 + 1x20


= 4 + 0 +1 = 5

PREETHA V AP/CSE, SRIT


Octal Numbers: Base 8

 Base or Radix is 8

 Digits: 0, 1, 2, 3, 4, 5, 6, 7

 Example:

1238 = 1x82 + 2x81 + 3x80


= 64 + 16 +3
= 83

PREETHA V AP/CSE, SRIT


Hexadecimal Numbers: Base
16
 Digits: 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F:
 A  10
 B  11
 C  12
 D  13
 E  14
 F  15
 Example:
1216 = 1x161 + 2x160
= 16 +2
= 18

PREETHA V AP/CSE, SRIT


Conversions

Decimal to Base N Base N to Decimal


Successive Division by N Multiplication with power of
(Remainder Method) N

PREETHA V AP/CSE, SRIT


Conversion of Decimal to Binary

2 25
2 12 -1
2 6-0
2 3-0
1-1

2510=110012
PREETHA V AP/CSE, SRIT
Binary to decimal conversion

11001
1X20 = 1
0X21 = 0
0X22 = 0
1X23 = 8
1X24 = 16
2510

PREETHA V AP/CSE, SRIT


Conversion of Decimal to Octal

8 125
8 15 - 5
1 -7

12510=1758

PREETHA V AP/CSE, SRIT


Example 2:

8 84
8 10 - 4
1 -2

8410=1248

PREETHA V AP/CSE, SRIT


Octal to decimal conversion

175

5X80 = 5
7X81 = 56
1X82 = 64
125

1758 = 12510
PREETHA V AP/CSE, SRIT
 Example 2:

124

4X80 = 4
2X81 = 16
1X82 = 64
84

1248 = 8410
PREETHA V AP/CSE, SRIT
Conversion of Decimal to Hexadecimal

16 450
16 28 - 2
1 - 12

45010=1C216

PREETHA V AP/CSE, SRIT


Example 2:
16 385
16 24 - 1
1 - 8

38510=18116

PREETHA V AP/CSE, SRIT


Hexadecimal to decimal conversion

1C2
2X160 = 2
12X161 = 192
1X162 = 256
450

1C216 = 45010

PREETHA V AP/CSE, SRIT


 Example 2:

1 81
1X160 = 1
8X161 = 128
1X162 = 256
385

18116 = 38510

PREETHA V AP/CSE, SRIT


Hexadecimal to Binary conversions
Example: Convert 2BA3 to Binary
Step 1: write the corresponding binary number for the given hexadecimal numbers
Step 2: combine all the binary numbers together.
2 B A 3
0010 1011 1010 0011

(0010101110100011)2

PREETHA V AP/CSE, SRIT


Other Conversions

 BINARYOCTAL
 BINARYHEXADECIMAL
 OCTALHEXADECIMAL etc,.

PREETHA V AP/CSE, SRIT


BINARYOCTAL
 OCTAL  BASE 8 = 23
 So separate as 3 digits
Example:110012  OCTAL
11 001

011 001
3 1

110012=318

PREETHA V AP/CSE, SRIT


Example 2:11100112  OCTAL

1 110 011

001 110 011


1 6 3

11100112=1638

PREETHA V AP/CSE, SRIT


Example:11100112  OCTAL

0111 0011

0111 0011
7 3

11100112= 73 16

PREETHA V AP/CSE, SRIT


Octal to binary
Convert (473)8 to binary
Step 1: Write the corresponding binary number for 4 7 and 3 separately
Step 2: Combine those binary numbers together.
4 7 3
100 111 011
(100111011)2

PREETHA V AP/CSE, SRIT


BINARYHEXADECIMAL
 HEXADECIMAL  BASE 16 = 24
 So separate as 4 digits
Example:110012  HEXADECIMAL
1 1001

0001 1001
1 9

110012=1916

PREETHA V AP/CSE, SRIT


OCTALHEXADECIMAL
 OCTALBINARYHEXADECIMAL
Example:318  HEXADECIMAL
3 1
011 001

0001 1001
1 9

318=1916

PREETHA V AP/CSE, SRIT


HEXADECIMAL  OCTAL
 Example: 1BD  OCTAL

1 B D
0001 1011 1101

000 110 111 101


0 6 7 5

1BD = 6758

PREETHA V AP/CSE, SRIT


Conversion from Decimal to Binary
25.125 10 Binary
2 25
2 12 -1
2 6-0
2 3-0
1-1

2510=110012
PREETHA V AP/CSE, SRIT
0.125 X 2 = 0.250
0.250 X 2 = 0.5
0.5 X 2 = 1.0

25.12510 = 11001.0012

PREETHA V AP/CSE, SRIT


Binary to decimal conversion

11 0 0 1 . 0 0 1

1X2-3 = 0.125
0X2-2 = 0
0X2-1 = 0
1X20 = 1
0X21 = 0
0X22 = 0
1X23 = 8
1X24 = 16
25.12510

PREETHA V AP/CSE, SRIT


Conversion from Decimal to Octal

84.250 10 Octal
8 84
8 10 - 4
1 -2

8410=1248

PREETHA V AP/CSE, SRIT


0.250 X 8 = 2.000

25.12310 = 124.28

PREETHA V AP/CSE, SRIT


Octal to decimal conversion

124.2

2X8-1 = 0.25
4X80 = 1
2X81 = 0
1X82 = 0
25.25010

PREETHA V AP/CSE, SRIT


Fundamentals of
algorithms,
Pseudocode, Flow
charts

PREETHA V AP/CSE, SRIT


Algorithm
 An algorithm is a finite sequence of instructions required for producing
the desired result.

PREETHA V AP/CSE, SRIT


Characteristics
 The steps in the algorithm must be unambiguous .
 It should be written in sequence.
 Ensure that the algorithm will terminate.
 It should conclude after a finite number of steps.

PREETHA V AP/CSE, SRIT


Factors used to judge the
algorithm
 Time
 Memory
 Accuracy
 Sequence etc,.

PREETHA V AP/CSE, SRIT


Representations
The algorithms can be represented in the following ways:
 Flowcharts
 Normal English
 Pseudo code etc,.

PREETHA V AP/CSE, SRIT


Example
 Addition of two numbers
Step1: Start
Step2: Read a, b
Step3: Add the value of a with the value of b and store the result in c.
Step4: Display the value of c
Step5: Stop

PREETHA V AP/CSE, SRIT


Flowcharts
 It is the pictorial representation of the algorithm.

PREETHA V AP/CSE, SRIT


Flowchart Symbols
 Terminal symbol
 It is used to represent the start, end of the program logic.

 Input/Output
 It is used for input or output.

 Process Symbol
 It is used to represent the calculations, data movements, initialization
operations etc,.

PREETHA V AP/CSE, SRIT


 Decision Symbol

 It is used to denote a decision to be made at that point.

 Flow lines
 It is used to connect the symbols.

 Connectors
 It is used to connect the flow lines.

PREETHA V AP/CSE, SRIT


Guidelines for preparing
flowcharts
 It should be simple.
 Standard symbols should be used.
 The flow lines should not intersect with each other.
 In case of complex flowcharts, make use of the connector symbol.

PREETHA V AP/CSE, SRIT


 Only one flow line should enter the process symbol and only one flow line
should come out from a process symbol.

 Only one flow line should be used with the terminal symbol.

START
STOP

PREETHA V AP/CSE, SRIT


 Only one flow line should enter the decision symbol and two or three flow
lines may leave from the decision symbol.

PREETHA V AP/CSE, SRIT


Benefits of Flowcharts
 Makes Logic Clear
 Communication
 Effective Analysis
 Useful in coding
 Useful in Testing etc,.

PREETHA V AP/CSE, SRIT


Limits of Flowcharts
 It is difficult to use flowcharts for large programs.
 Difficult to modify
 Cost etc,.

PREETHA V AP/CSE, SRIT


Pseudocode
 Pseudo means imitate and code means instruction.
 It is a formal design tool.
 It is also called Program Design Language.

PREETHA V AP/CSE, SRIT


Keywords
 READ,GET
 PRINT,DISPLAY
 COMPUTE,CALCULATE

PREETHA V AP/CSE, SRIT


Guidelines for writing Pseudocode

 Steps should be understandable


 Capitalize the keyword.
 Indent to show hierarchy.
 End multiple line structure etc,.

PREETHA V AP/CSE, SRIT


Example

GET a,b
COMPUTE c=a+b
PRINT c
stop

PREETHA V AP/CSE, SRIT


Example
READ a,b
IF a>b
PRINT a is greater
ELSE
PRINT b is greater
ENDIF
stop

PREETHA V AP/CSE, SRIT


Advantages & Disadvantage
 It can be easily modified
 It can be understood easily
 Compare to flowchart it is difficult to understand the program logic.

PREETHA V AP/CSE, SRIT


SELECTION CONTROL STRUCTURE
 It is used for making decisions.
 It allows the program to make a choice from alternative paths.
 IF …THEN
 IF …THEN… ELSE
 CASE etc.,

PREETHA V AP/CSE, SRIT


IF…THEN
Pseudocode Flow chart
IF condition THEN
process 1
.
. YES If
END IF
condition
.
.
Process 1 NO

PREETHA V AP/CSE, SRIT


IF…THEN…ELSE
Pseudocode Flowchart

IF condition THEN
process 1 YES If NO
. condition
.
ELSE
process 2 Process 1 Process 2
.
.
END IF
.
.

PREETHA V AP/CSE, SRIT


CASE structure
Pseudocode Flow chart
.
CASE Type yes Process 1
Type 1
Case Type-1:
Process 1 no
Case Type-2: yes
Process 2 Type 2 Process 2
.
no
.
yes
Case Type-n: Type 3 Process 3
Process n
. no
.
END CASE
PREETHA V AP/CSE, SRIT
Looping control structure
 It is used to execute some instructions several times based on some
condition.
 WHILE loop
 Do…WHILE loop etc.,

PREETHA V AP/CSE, SRIT


WHILE Loop
Pseudocode Flow chart
.
.
WHILE condition
no
. condition
.
Body of the loop
. yes
. Body of The loop
END WHILE

PREETHA V AP/CSE, SRIT


DO…WHILE Loop
Pseudocode Flow chart
DO
.
.
Body of The loop
Body of the loop
.
. yes
WHILE condition
condition
.
.
no

END WHILE
PREETHA V AP/CSE, SRIT
Example: Finding the area of a circle
Algorithm
Step1: Start
Step2: Read the value of r
Step3: Calculate area = 3.14*r*r
Step4: Print area
Step5: Stop

PREETHA V AP/CSE, SRIT


Pseudocode

Set area
READ the r
COMPUTE area=3.14*r*r
PRINT area
stop

PREETHA V AP/CSE, SRIT


Flowchart
START

Read r

area=3.14*r*r

Print area

STOP
PREETHA V AP/CSE, SRIT
Find the largest among three
Numbers
Algorithm
Step1: Start
Step2: Read the value of a, b, c
Step3: IF (a>b) and (a>c) THEN
print a is largest
ELSE IF (b>c) THEN
print b is largest
ELSE
print c is largest
Step4: Stop
PREETHA V AP/CSE, SRIT
Pseudocode
READ a, b, c
IF (a>b) and (a>c) THEN
WRITE a is largest
ELSE IF (b>c) THEN
WRITE b is largest
ELSE
WRITE c is largest
ENDIF
stop

PREETHA V AP/CSE, SRIT


Flowchart START

Read a,b,c

If yes
(a>b) and Print a
(a>c) Is largest
no
If yes
Print b
b>c
Is largest
no
Print c
Is largest
PREETHA V AP/CSE, SRIT

stop
Finding roots of the Quadratic
equation
Step:1 Start
Step:2 Enter the values of a,b,c
Step:3 Find the value of D Using the Formula,
D = b*b-4*a*c
Step:4 If D is greater than or equal to zero find 2
roots
root1(-b+sqrt(D))/(2*a)
root2(-b-sqrt(D))/(2*a)
Step:5 Print root1 & root2
Step:6 If D is less than zero, then print the root are
imaginary
Step:7 Stop
PREETHA V AP/CSE, SRIT
Pseudocode
Set root1,root2
READ the value of a, b, c
Find D b*b-4*a*c
IF D>=0 THEN
calculate root1=(-b+sqrt(D))/(2*a)
root2=(-b-sqrt(D))/(2*a)
ELSE
Roots are imaginary
END IF
WRITE root1,root2
Stop
PREETHA V AP/CSE, SRIT
Flow chart Start

Read a,b,c

D=b*b-4*a*c

no
If D>=0
yes

Root1=[-b+sqrt(D)]/(2*a)
Root2=[-b+sqrt(D)]/(2*a) Print
roots are imaginary

Print root1,root2
PREETHA V AP/CSE, SRIT

Stop
Finding the Factorial
Algorithm
Step1: Start
Step2: Read the value of n and set i =1
Step3: While i <= n do
fact =fact * i
i=i+1
else Goto step5
Step4: Goto step 3
Step5: print the value of fact
Step6: Stop
PREETHA V AP/CSE, SRIT
Pseudocode

READ the value of n and set i =1


WHILE (i <= n) do
fact =fact * i
i=i+1
ENDWHILE
Repeat the loop until condition fails
WRITE fact
stop

PREETHA V AP/CSE, SRIT


Flowchart Start

Read n
i=1

while no
i<=n
yes
fact=fact * i
i=i+1

Print fact

PREETHA V AP/CSE, SRIT


stop
Finding the Sum of the digits
Algorithm
Step1: Start
Step2: Read the value of n and set i = 0, sum = 0
Step3: While n>0 do
r=n%10
sum=sum + r
n=n/10
else Goto step5
Step4: Goto step 3
Step5: print the value of sum
Step6: Stop
PREETHA V AP/CSE, SRIT
Pseudocode

READ the value of n and set i =0, sum=0


WHILE (n>0) do
r=n%10
sum=sum + r
n=n/10
ENDWHILE
Repeat the loop until condition fails
WRITE sum
stop
PREETHA V AP/CSE, SRIT
Start
Flowchart
Read n

r = 0,sum=0

while no
n>0
yes

r=n%10
sum=sum + r
n=n/10

Print sum
PREETHA V AP/CSE, SRIT

stop
Finding the Reverse of a Number
Algorithm
Step1: Start
Step2: Read the value of n and set i = 0, sum = 0
Step3: While n>0 do
r=n%10
sum=sum *10 + r
n=n/10
else Goto step5
Step4: Goto step 3
Step5: print the value of sum
Step6: Stop
PREETHA V AP/CSE, SRIT
Pseudocode

READ the value of n and set i =0, sum=0


WHILE (n>0) do
r=n%10
sum=sum *10 + r
n=n/10
ENDWHILE
Repeat the loop until condition fails
WRITE sum
stop
PREETHA V AP/CSE, SRIT
Start
Flowchart
Read n

r = 0,sum=0

while no
n>0
yes

r=n%10
sum=sum *10 + r
n=n/10

Print sum
PREETHA V AP/CSE, SRIT

stop
Armstrong Number
Example: 153

13 +53 + 33 =153

PREETHA V AP/CSE, SRIT


Finding an Armstrong Number
Algorithm
Step1: Start
Step2: Read the value of n and set a = n, sum = 0
Step3: While n>0 do
r=n%10
sum=sum + r*r*r
n=n/10
else Goto step5
Step4: Goto step 3
Step5: If a = sum then
Print Armstrong Number
Else
Print It is Not an Armstrong Number
Endif
Step6: Stop

PREETHA V AP/CSE, SRIT


Pseudocode
READ the value of n and set a =n, sum=0
WHILE (n>0) do
r=n%10
sum=sum + r*r*r
n=n/10
ENDWHILE
Repeat the loop until condition fails
IF a=sum THEN
WRITE Armstrong Number
ELSE
WRITE It is not an Armstrong Number
ENDIF
stop

PREETHA V AP/CSE, SRIT


Start
Flowchart
Read n

a = n,sum=0

while
n>0 no
yes
r=n%10
sum=sum + r*r*r
n=n/10

if
a=sum
Print It is Not an
Print Armstrong No Armstrong No
PREETHA V AP/CSE, SRIT

stop
Finding the Fibonacci series
Algorithm
Step1: Start
Step2: Read the value of n and set f=0,f1=-1, f2=1
Step3: While (f<n) do
f=f1+f2
f1=f2
f2=f
Print f
else Goto step5
Step4: Goto step 3
Step5: Stop

PREETHA V AP/CSE, SRIT


Pseudocode

READ the value of n and set f=0 ,f1=-1, f2=1


WHILE (f<n) do
f=f1+f2
f1=f2
f2=f
WRITE f
ENDWHILE
Repeat the loop until condition fails
stop
PREETHA V AP/CSE, SRIT
Start
Flowchart
Read n

f=0,f1= -1,f2=1

while no
f<n
yes
f=f1+f2
f1=f2
f2=f

Print f

PREETHA V AP/CSE, SRIT

stop
Finding the sum of odd number between 1
to n
Algorithm
Step1: Start
Step2: Read the value of n and set sum=0,i=1
Step3: While (i<=n) do
sum=sum+i
i=i+2
else Goto step5
Step4: Goto step 3
Step5: Print sum
Step6: Stop

PREETHA V AP/CSE, SRIT


Pseudocode

READ the value of n and set sum=0,i=1


WHILE (i<=n) do
sum=sum+i
i=i+2
ENDWHILE
Repeat the loop until condition fails
WRITE sum
stop

PREETHA V AP/CSE, SRIT


Start
Flowchart
Read n

sum=0,i=1

While i<=n

sum=sum+i
i=i+2

Print sum

PREETHA V AP/CSE, SRIT


stop
Finding the sum of even number between 1
to n
Algorithm
Step1: Start
Step2: Read the value of n and set sum=0,i=0
Step3: While (i<=n) do
sum=sum+i
i=i+2
else Goto step 5
Step4: Goto step 3
Step5: Print sum
Step6: Stop

PREETHA V AP/CSE, SRIT


Pseudocode

READ the value of n and set sum=0,i=0


WHILE (i<=n) do
sum=sum+i
i=i+2
ENDWHILE
Repeat the loop until condition fails
WRITE sum
stop

PREETHA V AP/CSE, SRIT


Start
Flowchart
Read n

sum=0,i=0

While i<=n

sum=sum+i
i=i+2

Print sum

PREETHA V AP/CSE, SRIT


stop
Conversion of Binary number to Decimal

Algorithm
Step1: Start
Step2: Read the value of n and set i = 0, sum = 0
Step3: While n>0 do
r=n%10
sum=sum + r*pow(2,i)
n=n/10
i=i+1
else Goto step5
Step4: Goto step 3
Step5: print the value of sum
Step6: Stop
PREETHA V AP/CSE, SRIT
Pseudocode

READ the value of n and set i =0, sum=0


WHILE (n>0) do
r=n%10
sum=sum + r*pow(2,i)
n=n/10
i=i+1
ENDWHILE
Repeat the loop until condition fails
WRITE sum
stop PREETHA V AP/CSE, SRIT
Start
Flowchart
Read n

sum=0,i=0

While n>0
r=n%10
sum=sum + r*Pow(2,i)
n=n/10
i=i+1

Print sum

PREETHA V AP/CSE, SRIT


stop
PREETHA V AP/CSE, SRIT

You might also like