Algorithm and Flowchart
Algorithm and Flowchart
The Algorithm designed are language-independent, i.e. they are just plain
instructions that can be implemented in any language
Characteristics of an Algorithm
Recipe for preparing Indian Tea
Add ( a, b )
START
Step 1. Take two numbers from user say a & b.
Step 2. Add the value of a & b and store the result in c.
Step 3. Show the value of c.
End
Algorithm to Swap two numbers with third
variable
Swap_with_Third_Var ( a, b )
This algorithm interchange / swap the values of two variables such as a & b.
START
Step 1. Take two numbers from user say a & b.
Step 2. Store the value of a into c.
Step 3. Store the value of b into a.
Step 4. Store the value of c into b.
Step 5. Show the value of a & b.
End
Algorithm to Swap two numbers without
third variable
Swap_without_Third_Var ( a, b )
This algorithm interchange / swap the values of two variables such as a & b.
START
Step 1. Take two numbers from user say a & b.
Step 2. Find a + b & Store the result into a.
Step 3. Find a - b & Store the result into b.
Step 4. Find a - b & Store the result into a.
Step 5. Show the value of a & b.
End
Algorithm to find whether a given number
is Even or Odd
EVEN_ODD ( n )
START
Step 1. Take a number from user say n.
Step 2. Compute n/2 & store the remainder in r.
Step 3. if value of r is 0 then
Show : “n is EVEN”
Step 4. Otherwise
Show : “n is ODD”
End
Algorithm to find greatest number among
two numbers
GREATEST_TWO_NUMBERS ( a, b )
START
Step 1. Take two numbers from user say a & b.
Step 2. if value of a is greater than value of b then
Show : “a is greatest”
Step 3. Otherwise
Show : “b is greatest”
End
Algorithm to find greatest number among
Three numbers
GREATEST_THREE_NUMBERS ( a, b, c )
START
Step 1. Take three numbers from user say a, b & c.
Step 2. if a > b and a > c then
Show : “a is greatest”
Step 3. Otherwise if b > a and b > c then
Show : “b is greatest”
Step 4. Otherwise
Show : “c is greatest”
End
Algorithm to Print 1 to 10
PRINT_1_TO_10 ( )
START
Step 1. Set i = 1
Step 2. Repeat Step 3 to Step 4 until i <= 10 do
Step 3. Print : i
Step 4. Set i = i + 1
END
Algorithm to Print Table of 2
PRINT_TABLE_2 ( )
START
Step 1. Set i = 2
Step 2. Repeat Step 3 to Step 4 until i <= 20 do
Step 3. Print : i
Step 4. Set i = i + 2
END
Algorithm to Print Table of 2 (Another
Method)
PRINT_Table_2 ( )
START
Step 1. Set i = 1
Step 2. Repeat Step 3 to Step 4 until i <= 10 do
Step 3. Print : 2 * i
Step 4. Set i = i + 1
END
Algorithm to Print Table of n
PRINT_Table_n ( n )
START
Step 1. Take a number from user say n.
Step 2. Set i = 1
Step 3. Repeat Step 4 to Step 5 until i <= 10 do
Step 3. Print : n * i
Step 4. Set i = i + 1
END
Flowchart
Terminals
represented by rounded rectangles / Oval
indicate a starting or ending point
START
END
Basic Flowchart Symbols
Input/Output Operations
represented by parallelograms
indicate an input or output operation
Display message
“How many
Read Hours
hours did you
work?”
Basic Flowchart Symbols
Processes
represented by rectangles
indicates a process such as a mathematical computation or
variable assignment
Multiply Hours
by PayRate.
Store result in
GrossPay.
Basic Flowchart Symbols
Line / Arrow
Used to show the flow of data
Basic Flowchart Symbols
Connector
represented by Circle
Used to connect the part of flowchart / segments
of flowchart A
START
END
A
Basic Flowchart Symbols
Decision Making
Represented by rhombus
Used to make decision / to select one path
according to condition
Flowchart to find sum of two numbers
Start
Read : N1, N2
Sum = N1 + N2
Print : Sum
End
Flowchart to swap two numbers
Start
Read : N1, N2
N3 = N1
N1 = N2
N2 = N3
Print : N1, N2
End
Flowchart to swap two numbers without
Third Variable
Start
Read : N1, N2
N1 = N1 + N2
N2 = N1 – N2
N1 = N1 – N2
Print : N1, N2
End
Flowchart to Find Even or Odd
Start
Read : N1
YES If NO
N1%2
== 0
End
Flowchart to Find Greatest among two
Numbers
Start
Read : N1, N2
YES NO
If
N1>N2
End
Flowchart to Find Greatest among three
Numbers
Start
YES If If
NO
N1>N2 N2>N1
and and
N1>N3 N2>N3
End
Flowchart to Find Greatest among three Numbers
Start
If If NO
N1>N2 N2>N3
NO
YES
YES
If
N1>N3
NO Print : “N2 is Print : “N3 is
Greatest” Greatest”
YES
Print : “N1 is Print : “N3 is
Greatest” Greatest”
End
Algorithm to Print 1 to 10
PRINT_1_TO_10()
This algorithm prints 1 to 10.
Begin
1. Print : 1
2. Print : 2
3. Print : 3
4. Print : 4
.
.
.
10. Print : 10
End
Algorithm to Print 1 to 10
PRINT_1_TO_10()
This algorithm prints 1 to 10.
Begin
1. Set i =1
2. Print : i
3. Set i = i+1
4. Print : i
5. Set i = i+1
.
.
10. Print : i
11. Set i = i+1
End
Algorithm to Print 1 to 10
PRINT_1_TO_10()
This algorithm prints 1 to 10.
Begin
1. Set i = 1
2. Repeat step 3 to 4 until i<=10 do
3. print : i
4. Set i = i+1
End
Flowchart to print 1 to 10
START
Set i = 1
If
i<=1
0
Print : i
STOP
Set i = i+1
Algorithm to Print the Table of 2
PRINT_TABLE_TWO()
This algorithm prints the table of 2.
Begin
1. Set i = 2
2. Repeat step 3 to 4 until i<=20 do
3. print : i
4. Set i = i+2
End
Algorithm to Print the Table of 2
PRINT_TABLE_TWO()
This algorithm prints the table of 2.
Begin
1. Set i = 1
2. Repeat step 3 to 4 until i<=10 do
3. print : 2*i
4. Set i = i+1
End
Flowchart to print Table of 2
START
Set i = 1
If
i<=1
0
Print : 2*i
STOP
Set i = i+1
Algorithm to Print the Table of n
PRINT_TABLE_N()
This algorithm prints the table of N.
Begin
1. Take a value from user, say n
2. Set i = 1
3. Repeat step 3 to 4 until i<=10 do
4. print : n*i
5. Set i = i+1
End
Flowchart to print Table of N
START
Read : n
Set i = 1
If
i<=1
0
Print : n*i
STOP
Set i = i+1
Algorithm to Print Even Numbers between
1 to 100
PRINT_EVEN_1_TO_100()
This algorithm prints Even numbers between 1 to
100.
Begin
1. Set i = 1
2. Repeat step 3 to 4 until i<=100 do
3. if i%2 == 0 then
print : i
4. Set i = i+1
End
Flowchart to Print Even Numbers between
1 to 100
START
Set i = 1
NO If YES NO
If
i%2 ==
i<=100
0
YES
Print : i STOP
Set i = i+1
Algorithm to find the factorial of a given
Number
FACTORIAL(n)
This algorithm finds the factorial of a given number.
Begin
1. Take a value from user, say n
2. set fact=1 and i=2
3. Repeat step 4 to 5 until i<=n do
4. set fact = fact * i
5. set i = i + 1
6. print : fact
End
Flowchart to find the factorial of a given
Number
START
Read : n
If
i<= Print : fact STOP
n
Read : n NO
If n%i
== 0
Set i=2 and
count = 0
YES
Set count=count+1
YES If If YES
Print :
count i<=
“PRIME”
==1 NO n Set i = i + 1
NO
Machine Language
Assembly Language
Advantage:
Translation Free
High Speed
Disadvantage:
Machine Dependent
Complex Language
Assembly Language
Advantages
Easy to understand
Less error prone
Disadvantages
Machine Dependent
Harder to learn
High Level Language
Program statement are not closely related to the internal
characteristics of the computer.
Programs are machine independent.
Advantage
Machine independent
Readability
Easy Debugging
Disadvantage
Less Efficient
Poor control on hardware
Translators
Assembler
Compiler
Interpreter