Professional Documents
Culture Documents
Class Notes On Algorithms and Flowcharts
Class Notes On Algorithms and Flowcharts
On
ALGORITHMS
And
FLOWCHARTS
Class Notes on ALGORITHMS and FLOWCHARTS
ALGORITHMS
Definition:
The finite sequence of steps that a process performs in order to solve a problem is known as an
algorithm. Algorithms are used for calculation, data processing, and several other activities in the
fields of mathematics, computer science and other related subjects.
Properties of algorithms:
1. An algorithm is a step by step procedure.
2. The number of steps in an algorithm is finite.
3. An algorithm is result oriented, i.e. it has a purpose and gives certain results.
4. The execution of an algorithm stops after in a definite time.
5. Each step is unambiguous, i.e. each step has a well defined meaning.
6. An algorithm can accept inputs and give outputs.
Expressing algorithms:
Algorithms can be expressed in the following ways:
1. Natural Language
2. Flowcharts
3. Pseudocode
4. Programming languages
Example :
Algorithm to find sum and average of two numbers —
FLOWCHARTS
Definition:
A flowchart is a graphical representation of an algorithm. Each step in the process is represented by
a defined symbol and contains a short description of the process step. The symbols are linked
together with arrows that show the direction of the process flow. Since flowcharts have a pictorial
format, they are often easier to understand than the textual formats.
Example:
Flowchart to find sum and average of two numbers.
START
INPUT x, y
sum = x + y
average = sum / 2
PRINT Average
END
DATA ITEMS
DATA TYPES
Data types determine the type of values that variables and constants may have. The common data
types supported in most programming languages are:
a) Integer
Integers are numeric data items, which are either positive or negative including zero. For
example 1, 48888, -56, 0, 456 etc. Some programming languages put restrictions on the
magnitude of integers which may be used in program instructions.
b) Real Numbers
There are two types of real numbers, fixed-point and floating-point.
Fixed Point
Fixed point data items are numbers which have embedded decimal point.
For example: 1.5, 458.4589, -0.569 etc
Floating Point
Floating point data items are numbers, which are, held as binary fractions by a computer. The
numbers have a mantissa and an exponent.
For example:
Number Mantissa Exponent
62.5 = 0.625 × 102 0.625 2
62500 = 0.625 × 105 0.625 5
0.0000625 = 0.625 × 10-4 0.625 -4
d) Boolean
Boolean data items are used as status indicators and may contain only one of two possible
values, True or False.
OPERATORS
ASSIGNMENT OPERATOR
MATHEMATICAL OPERATORS
a) Unary Mathematical Operators
The unary operators operate on one operand. The unary mathematical operators are:
Increment Operator: Increases the value of a number by 1. For example, x++
Decrement Operator: Decreases the value of a number by 1. For example, x--
RELATIONAL OPERATORS
Relational operators allow to compare numeric values or character values. They determine whether
one is greater than, less than, equal to, or not equal to another.
The relational operators are:
Relational Operators Meaning
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
= or == Equal to
!= Not equal to
Prepared by Moumita Mitra Manna
Dept. of Computer Science, Bangabasi College
Page |6
Class Notes on ALGORITHMS and FLOWCHARTS
Example:
Let us consider that x := 5, y := 10, z := 15. The values of the following expressions will be:
( z > y ) : returns True
( (y – x) == (z – y)) : returns True
( y < x ) : returns False
( z != x ) : returns True
LOGICAL OPERATORS
Logical operators connect two or more relational expressions into one or reverse the logic of an
expression.
OPERATOR MEANING
AND connects two expressions into one. Both expressions must be true for the
AND
overall expression to be true.
OR connects two expressions into one. At least one of the expressions must be
OR
true for the overall expression to be true.
NOT operator reverses the "truth" of an expression. It makes a true expression
NOT
false, and a false expression true.
Example:
Let us consider that x := 5, y := 10, z := 15. The values of the following expressions will be:
( z > 10 ) AND ( y > 5 ) : returns True
( z > 10 ) AND ( y < 5 ) : returns False
( z > 10 ) OR ( y < 5 ) : returns True
( z < 10 ) OR ( y < 5 ) : returns False
NOT ( z < 20 ) : returns False
NOT ( z > 20 ) : returns True
Decision control is a situation in the algorithm where one has to make a choice of two alternatives
flow of controls by making decision depending on a given condition. They are also called selection
structures.
Depiction in flowchart:
Example
Task_if_True Task_if_False
PRINT “Positive” PRINT “Negative”
Example:
Algorithm to test whether a number is positive or negative —
LOOPS
Loops allow repeated execution of certain portions of the algorithm subject to certain conditions.
Each execution within the loop is called iteration. There are two types of loops:
Entry Controlled Loops: In entry controlled loops, the conditions are checked before the
control enters the loop in each iteration.
Exit Controlled Loops: In exit controlled loops, the conditions are checked before the
control exits from the loop in each iteration.
Statements within
condition the body of the
loop
False
True
Statements within
condition
the body of the
loop
False
True
Kinds of Loops:
While Loop
Do – While Loop
For Loop
WHILE LOOP
While loop is an entry controlled loop, where the iterations are subject to fulfilment of certain
conditions.
Syntax:
WHILE condition
Statement1
Statement 2
….. …..
( End of while loop )
Example:
Algorithm to find H.C.F. of two numbers —
DO WHILE LOOP
Do while loop is an exit controlled loop, , where the iterations are subject to fulfilment of certain
conditions.
Syntax:
DO
Statement1
Statement 2
….. …..
WHILE condition
Example:
Algorithm to find the sum of the following series:
1 1 1 1 1
+ + + + + … … … … … … … … … ..
1! 2! 3! 4! 5!
Output : Sum of the series
Step - 1. INITIALISE sum := 0, number := 1
Step - 2. REPEAT
(i) INITIALISE factorial := 1, i := 1
(ii) WHILE i <= number
a) factorial := factorial * i
b) INCREMENT i
(End of While loop)
(iii) term := 1 / factorial
(iv) sum := sum + term
(v) INCREMENT number
WHILE term > 0.000001
Step - 3. PRINT sum
Step - 4. END
FOR LOOP
For Loop is an entry controlled loop which is used when the number of iterations is known in
advance. This, in its simplest form, uses an initialisation of the variable as a starting point, a stop
condition depending on the value of the variable. The value of the variable is changed on each
iteration until it reaches the required value.
Syntax:
FOR (initial_state TO terminal_condition, change_in_each_iteration)
Statement1
Statement 2
….. …..
(End of For Loop)
Example:
Algorithm to test whether a number is prime or not:
Sample Algorithms
Problem 1:
Write an algorithm to evaluate the sum of an A. P. Series.
Solution:
Algorithm to find the sum of an A.P. Series:
Problem 2:
Write an algorithm to display all the Fibonacci numbers within a range. For example, the Fibonacci
numbers within 20 are:
0, 1, 1, 2, 3, 5, 8, 13
Solution:
Algorithm to display the Fibonacci Series:
Problem 3:
Write an algorithm to input marks in 5 subjects and then find the grade obtained by the student
where grade is given by the following rule:
Average Marks Grade
Average ≥ 80 A
65 ≤ Average < 80 B
50 ≤ Average < 65 C
35 ≤ Average < 50 D
Average < 35 Fail
Solution:
Algorithm to find grade:
Problem 4
Write an algorithm to display the prime factors of a number.
Solution
Algorithm for Function isPrime():
Problem 5
Write an algorithm in C to compute the roots of a quadratic equation.
Solution
Algorithm to find the roots of a quadratic equation
Inputs : The coefficients of the equation a, b, c
Outputs : The roots of the equation
Step 1:- INPUT the coefficients of the quadratic equation, a, b, c
Step 2:- IF a = 0 THEN
1. PRINT “ This is not a quadratic equation.”
2. EXIT
[ End of if structure]
Step 3:- COMPUTE the determinant d = (b*b)-(4*a*c)
Step 4:- IF (d > 0) THEN
1. PRINT “The roots are real and distinct.”
2. COMPUTE the first root x = ((-b + sqrt(d)) / 2 * a)
3. COMPUTE the second root y= ((-b- sqrt(d)) /2 * a)
4. PRINT x and y.
Step 5:- ELSE IF (d = 0) THEN
1. PRINT “ The roots are equals.”
2. COMPUTE the roots x = y = ( -b / (2 * a))
3. PRINT x.
Step 6:- ELSE
1. ASSIGN d = -d
2. PRINT “The roots are imaginary.”
3. COMPUTE s = (- b / (2 * a))
4. COMPUTE m = ( sqrt(d) / 2 * a)
5. The first root x = s + im and second root y = s – im
6. PRINT the values of x and y.
[ End of if… Then…Else if structure]