INTRODUCTION TO ALGORITHMS AND FLOWCHART
ALGORITHM AND ITS CHARACTERISTICS
Algorithm:
Algorithm is a step by step procedure, which defines a set of
instructions to be executed in certain order to get the desired
output. Algorithms tell the programmers how to code the program.
Algorithms are generally analyzed on two factors − time and space.
That is, how much execution time and how much extra space
required by the algorithm.
Characteristics of Algorithm:
Input − An algorithm should have 0 or more well-defined inputs.
Output − An algorithm should have 1 or more well-defined outputs,
and should match the desired output.
Finiteness − Algorithms must terminate after a finite (countable)
number of steps.
Definiteness / Unambiguous − Algorithm should be clear and
unambiguous. Each of its steps (or phases), and their inputs/outputs
should be clear and must lead to only one meaning.
Effectiveness- It is measured in terms of time and space.
Algorithm Notation
Name of algorithm: Specifies problem to be solved. It should be in
capital letters.
Step no - Identification tag of an instruction and it is an unsigned
position number along with explanatory comment.
Termination
Control Structures used in Algorithms:
An algorithm uses three control structures 1. Sequence 2.
Decision 3. Repetition
Sequence: Sequence means that each step of the algorithm is
executed in a specified order.
Example: This algorithm performs the steps in a purely sequential
order.
Problem − Design an algorithm to add two numbers and
display the result.
Algorithm: ADD:
Step 1 − START
Step 2 − Input the first number as A
Step 3 − Input the second number as B
Step 4 − Set C ← A + B
Step 5 – Display C
Step 6 – STOP
Decision: Decision statements are used when the execution of a
process depends on some condition. The general form of "if" is
if condition then if condition
then
Process OR
Process1
else
Process2
This form of decision is known as if - else construct. Here, If the
condition is true then process1 is executed else process2 is
executed.
Example Problem − Design an algorithm to add two numbers
and display the result.
Algorithm: EQUALITY_OF_2_NUMBERS:
Step 1 − START
Step 2 − Input the first number as A
Step 3 − Input the second number as B
Step 4 − if A = B then
Display "Equal"
else
Display "Not Equal"
Step 5 – STOP
Repetition: Repetition statements are used to execute one or more
steps for a number of times. Repetition can be implemented using 3
constructs, while, do-while and for loop. These loops execute one or
more steps until some condition is true.
Example Problem − Design an algorithm to print first 10
natural numbers
Algorithm: NATURAL_NUMBERS:
Step 1 − START
Step 2 − [Initialize] set I = 1, N = 10
Step 3 − Repeat steps 4 and 5 while I <= N
Step 4 − Print I
Step 5 – Set I = I + 1
Step 6 – STOP
Some example algorithms
Write an algorithm to find the largest among three different
numbers entered by user.
Step 1: Start
Step 2: Declare variables a, b and c.
Step 3: Read variables a, b and c.
Step 4: IF a > b THEN
IF a > c THEN
Display a is the largest number.
Else
Display c is the largest number.
Else
IF b > c THEN
Display b is the largest number.
Else
Display c is the greatest number.
Step 5: Stop
Write an algorithm to find all roots of a quadratic equation
ax2+bx+c=0.
Step 1: Start
Step 2: Declare variables a, b, c, D, x1, x2, rp and ip;
Step 3: Calculate discriminate
D ←b2-4ac
Step 4: If D≥0
r1← (-b+√D)/2a
r2← (-b-√D)/2a
Display r1 and r2 as roots.
Else
Calculate real part and imaginary part
rp ←b/2a
ip ←√(-D)/2a
Display rp+j(ip) and rp-j(ip) as roots
Step 5: Stop
Write an algorithm to find the factorial of a number entered
by user.
Step 1: Start
Step 2: Declare variables n, factorial and i.
Step 3: Initialize variables
factorial←1
i←1
Step 4: Read value of n
Step 5: Repeat the steps until i=n
5.1: factorial ←factorial * i
5.2: i←i+1
Step 6: Display factorial
Step 7: Stop
Write an algorithm to check whether a number entered by
user is prime or not.
Step 1: Start
Step 2: Declare variables n, i, flag.
Step 3: Initialize variables
flag←1
i←2
Step 4: Read n from user.
Step 5: Repeat the steps until i<(n/2)
5.1 If remainder of n÷i equals 0
flag←0
Go to step 6
5.2 i←i+1
Step 6: If flag=0
Display n is not prime
else
Display n is prime
Step 7: Stop
Write an algorithm to find the Fibonacci series till term
≤1000.
Step 1: Start
Step 2: Declare variables first_term,second_term and temp.
Step 3: Initialize variables first_term←0 second_term←1
Step 4: Display first_term and second_term
Step 5: Repeat the steps until second_term≤1000
5.1: temp←second_term
5.2: second_term←second_term+first term
5.3: first_term←temp
5.4: Display second_term
Step 6: Stop
Keywords used in Pseudo Code:
In Pseudo code for decision making and looping the designer must
use the keywords like If --- EndIf, Case --- EndCase, While --
EndWhile, Do While --- EndDo, Do Until --- EndDo, Call --- with
(Parameters), Call Return ---; Return, When and so on.
IF -- ENDIF: It is for execute a sequence based on a condition.
IF condition THEN IF age >= 18 THEN
Sequence 1; display Eligible to
vote
ELSE ELSE
Sequence 2; display Not Eligible
ENDIF ENDIF
WHILE: It specifies a loop that tests a condition at the top and
executes the sequence if the condition is true. After each iteration
the condition will be tested and the loop will be executed as long as
the condition is true.
WHILE condition WHILE I < 10
Sequence Print I
ENDWHILE Increment I
ENDWHILE
CASE: It is used to construct a multi way branch based on condition
that are mutually exclusive.
CASE expression OF CASE day OF
condition 1: sequence-1; 1: print "Sunday"
condition 2: sequence-2; 2: print "Monday"
. 3: print "Tuesday"
. 4: print "Wednesday"
condition n: sequence-n; 5: print "Thursday"
OTHERS: default: sequence; 6: print "Friday"
ENDCASE; 7: print "Saturday"
ENDCASE
REPEAT: This is similar to WHILE loop, except that the test condition
is performed at the end of loop.
REPEAT REPEAT
sequence print I
UNTIL condition Increment I
UNTIL I <= 10
FOR: It is used for iterating a sequence for a specific number of
times.
FOR iteration bounds FOR each student in the class
sequence Add 10 as bonus marks
ENDFOR ENDFOR