ALGORITHM AND FLOWCHART
Algorithm:
In programming, algorithm is a set of well defined instructions in sequence to solve the problem.
Properties of algorithm
Donald Ervin Knuth has given a list of five properties for an algorithm, these properties are:
1) Finiteness: An algorithm must always terminate after a finite number of
steps. It means after every step one reach closer to solution of the problem
and after a finite number of steps algorithm reaches to an end point.
2) Definiteness: Each step of an algorithm must be precisely defined. It is
done by well thought actions to be performed at each step of the algorithm.
Also the actions are defined unambiguously for each activity in the algorithm.
3) Input: Any operation you perform need some beginning value/quantities
associated with different activities in the operation. So the value/quantities
are given to the algorithm before it begins.
4) Output: One always expects output/result (expected value/quantities) in
terms of output from an algorithm. The result may be obtained at different
stages of the algorithm. If some result is from the intermediate stage of the
operation then it is known as intermediate result and result obtained at the
end of algorithm is known as end result. The output is expected
value/quantities always have a specified relation to the inputs.
5) Effectiveness: Algorithms are to be developed/written using basic
operations. The operations should be basic enough, so that they can be done
in a finite amount of time by a person, by using paper and pencil only.
SOLVED EXAMPLES
Problem 1: Find the area of a Circle of radius r.
Inputs to the algorithm: Radius r of the Circle.
Expected output: Area of the Circle
Algorithm:
Step1: Start
Step2: Read/input the Radius r of the Circle
Step3: Area=PI*r*r //calculation of area of the circle where PI=3.14
Step4: Print Area
Step5: Stop
Problem2: Write an algorithm to read two numbers and find their sum.
Inputs to the algorithm: Two numbers, num1 and num2
Expected output: Sum of the two numbers.
Algorithm:
Step1: Start
Step2: Read/input num1 and num2.
Step3: Sum=num1+num2 // calculation of sum
Step4: Print Sum
Step5: End
Problem 3: write algorithm to find the greater number between two numbers
Inputs to the algorithm: Two numbers, A and B
Expected output: Greater number between A and B.
Algorithm:
Step1: Start
Step2: Read/input A and B
Step3: If A greater than B then C=A and goto Step5, else goto Step4
Step4: C=B
Step5: Print C
Step6: End
Problem 4: Find the greatest number among three numbers.
Inputs to the algorithm: Three numbers, a, b and c
Expected output: Greatest number among a, b and c.
Algorithm:
Step 1 : Start
Step 2 : Input a, b, c
Step 3 : if a > b goto step 4, otherwise goto step 5
Step 4 : if a > c goto step 6, otherwise goto step 8
Step 5 : if b > c goto step 7, otherwise goto step 8
Step 6 : Output "a is the largest", goto step 9
Step 7 : Output "b is the largest", goto step 9
Step 8 : Output " c is the largest", goto step 9
Step 9 : Stop
Problem 5: Find GCD of two numbers.
Inputs to the algorithm: Two numbers, a and b
Expected output: GCD of a and b
Algorithm:
Step 1 : Start
Step 2: input a and b
Step 3: Let r=a mod b
Step 4: Let a = b and b = r
Step 5: Repeat Steps 3 and 4 until a mod b is greater than 0
Step 6: GCD = b
Step 7: Stop
We can also use Pseudo code to write the algorithm. We can use programming language constructs
to write pseudo codes.
Example 1: Write pseudo code to print all multiples of 5 between 1 and 100 (including both 1 and
100).
Set x=1
while(x<=100)
{
Print x
x=x*5
}
Example 2: Sort a set of values using Bubble sort algorithm
Initialize n=length of Array
BubbleSort(Array,n)
{
for i=0 to n-2
{
for j=0 to n-2
{
if Array[j]>Array[j+1]
{
swap(Array[j],Array[j+1])
}
}
}
}
FLOWCHART
The flowchart is a diagram which visually presents the flow of data through processing systems. This
means by seeing a flow chart one can know the operations performed and the sequence of these
operations in a system.
FLOWCHART SYMBOLS:
Symbol Name Function
Indicates any type of internal
Process operation inside the Processor or
Memory
Used for any Input / Output (I/O)
input/output operation. Indicates that the
computer is to obtain data or output
results
Used to ask a question that can be
Decision answered in a binary format (Yes/No,
True/False)
Allows the flowchart to be drawn
Connector without intersecting lines or
without a reverse flow.
Used to invoke a subroutine or an
Predefined Process Interrupt program.
Indicates the starting or ending of the
Terminal program, process, or interrupt
program
Flow Lines Shows direction of flow.
Solved Examples:
Problem 1: Find the area of a circle of radius r.
Problem 2: Convert temperature Fahrenheit to Celsius.
Problem 3: Find the greater number between two numbers.
Problem 4: Find the greatest one among three numbers.
Problem 5: Find the GCD of two numbers.
Start
Read two numbers a, b
False
c=a mod b
True
Is c=0? a=b, b=c
Print c as result
Stop
Problem 6: Find sum of all even numbers from 1 to n.
Start
Read n
Let i=1
sum=0
Is i=n? True
False
Is i % 2=0? False
True
sum=sum+i
i=i+1
Print sum as result
Stop