Algorithm Flow Notes
Algorithm Flow Notes
Any programming language is implemented on a computer. Right form its inception, to the present
day, all computer system (irrespective of their shape & size) performs the following 5 basic
operations. It converts the raw input data into information, which is useful to the users.
➢ Input: It is the process of entering data & instructions to the computer system.
➢ Storing: The data & instructions are stored for either initial or additional processing,
as & when required.
➢ Processing: It requires performing arithmetic or logical operation on the saved data
to convert it into useful information.
➢ Output: It is the process of producing the output data to the end user.
➢ Controlling: The above operations have to be directed in a particular sequence to
be completed.
➢ Storage Unit: The data & instruction that are entered have to be stored in the
computer. Similarly, the end results & the intermediate results also have to be
stored somewhere before being passed to the output unit. The storage unit
provides solution to all these issues. This storage unit is designed to save the initial
data, the intermediate result & the final result. This storage unit has 2 units:
Primary storage & Secondary storage.
Primary Storage: The primary storage, also called as the main memory, holds the data when the
computer is currently on. As soon as the system is switched off or restarted, the information held
in primary storage disappears (i.e. it is volatile in nature). Moreover, the primary storage normally
has a limited storage capacity, because it is very expensive as it is made up of semiconductor
devices.
Secondary Storage: The secondary storage, also called as the auxiliary storage, handles the
storage limitation & the volatile nature of the primary memory. It can retain information even when
the system is off.
➢ Central Processing Unit: Together the Control Unit & the Arithmetic Logic Unit are
called as the Central Processing Unit (CPU). The CPU is the brain of the computer.
Like in humans, the major decisions are taken by the brain itself & other body parts
function as directed by the brain. Similarly in a computer system, all the major
calculations & comparisons are made inside the CPU. The CPU is responsible for
activating & controlling the operation of other units of the computer system.
Arithmetic Logic Unit: The actual execution of the instructions (arithmetic or logical operations)
takes place over here. The data & instructions stored in the primary storage are transferred as &
when required.
Control Unit: This unit controls the operations of all parts of the computer but does not carry out
any actual data processing.It is responsible for the transfer of data and instructions among other
units of the computer.It manages and coordinates all the units of the system.It also communicates
with Input/Output devices for transfer of data or results from the storage units.
➢ Output Unit: The job of an output unit is just the opposite of an input unit. It accepts
the results produced by the computer in coded form. It converts these coded
results to human readable form. Finally, it displays the converted results to the
outside world with the help of output devices ( Eg :monitors, printers, projectors
etc..).
➢ Hardware- This hardware is responsible for all the physical work of the computer.
➢ Software- This software commands the hardware what to do & how to do it.
System Software- System software are a set of programs, responsible for running the computer,
controlling various operations of computer systems and management of computer resources.
They act as an interface between the hardware of the computer & the application software. E.g.:
Operating System
Algorithm :
Algorithm is a finite Step by step instructions to solve a problem. It is written
in normal English.
Advantages of Algorithms:
• It provides the core solution to a given problem. The solution can be implemented on a
computer system using any programming language of user’s choice.
• It facilitates program development by acting as a design document or a blue print of a given
problem solution.
• It ensures easy comprehension of a problem solution as compared to an equivalent
computer program.
• It eases identification and removal of logical errors in a program.
• It facilitates algorithm analysis to find out the most efficient solution to a given problem.
Examples
Purpose of a Flowchart:
Provides Communication.
Provides an Overview.
Shows all elements and their relationships.
Quick method of showing Program flow.
Checks Program logic.
Facilitates Coding.
Provides Program revision.
Provides Program documentation.
Advantages of a Flowchart :
Flowchart is an important aid in the development of an algorithm itself.
Easier to Understand the Program itself.
Independent of any particular programming language.
Proper documentation.
Proper debugging.
Easy and Clear presentation.
Limitations of a Flowchart :
Complex logic.
Drawing is time consuming.
Difficult to draw and remember.
Technical detail.
Explain the Symbols in a Flowchart ?
with flowchart , essential steps of an algorithm are shown using the shapes
above. The flow of data between steps is indicated by arrows or flow lines.
Step 1 : Start
Step 2 : Input num1 , num2
Step 3 : temp = num1
Step 4 : num1 = num2
Step 5 : num2 = temp
Step 6 : Output num1 , num2
Step 7 : Stop
Step 1 : Start
Step 2 : Input num1 , num2
Step 3 : calculate num1 = num1 + num2
Step 4 : calculate num2 = num1 – num2
Step 5 : calculate num1 = num1 – num2
Step 6 : Output num1 , num2
Step 7 : Stop
Flowcharts :
Write an algorithm and flowchart to find the largest among two numbers.
Ans:
Algorithm:
Step 1 : Start
Step 2 : Input A , B
Step 3 : if A > B then output A
else output B
Step4: Stop
Flowchart :
Write an algorithm and flowchart to find the largest among three numbers.
a.Algorithm: (method 1)
Step 1 : Start
Step 2 : Input A, B, C
Step 3 : if A > B go to step 4 , otherwise go to step 5
Step 4 : if A > C go to step 6 , otherwise go to step 8
Step 5 : if B > C go to step 7, otherwise go to step 8
Step 6 : print “ A is largest ” , go to step 9
Step 7 : print “ B is largest ”, go to step 9
Step 8 : print “ C is largest ” , go to step 9
Step 9 : Stop
Flowchart:
b.Algorithm:
. Step 1 : Start
Step 2 : Input A, B, C
Step 3 : Let max = A
Step 4 : if B > max then max = B
Step 5 : if C > max then max = C
Step 6 : output max is largest
Step 7 : Stop
Flowchart:
Algorithm:
Step 1: Start
Step 2: Input n
Step 3: Initialize counter variable, i , to 1 and factors = 1
Step 4: if i <= n go to step 5 otherwise go to step 7
Step 5: calculate factors = factors * i
Step 6: increment counter variable, i, and go to step 4
Step 7: output factors.
Step 8: stop
Flow chart:
Hint: A number is said to be prime number for which the only factors are 1 and itself
Algorithm:
Step 1: Start
Step 2: Input n
Step 3: Let i = 1, count=0
Step 4: if i > n/2 go to step 7
Step 5: if (n % i = = 0) count = count + 1
Step 6: increment i and go to step 4
Step 7: if count=2 then print “prime number” else print “not prime
number”
Step 8: Stop
Flow chart:
Algorithm:
Step 1: Start
Step 2: Input n
Step 3: Let rev=0
Step 4: if n<=0 go to step 8
Step 5: digit = n % 10
Step 6: rev = (rev * 10) + digit
Step 7: n = n / 10 then go to step 4
Step 8: output rev
Step 9: Stop
Write an algorithm and flowchart to find the Sum of individual digits if a given number
Algorithm :-
Step 1 : Start
Step 2 : read n
Step 3 : Sum = 0
Step 4 : While n > 0 do
4.1 : r = n % 10
4.2 : Sum = Sum + r
4.3 : n = n/10
Step 5 : End while
Step 6 : Write Sum
Step 7 : Stop
Flowchart :-