0% found this document useful (0 votes)
34 views

Algorithm and Flowchart

Uploaded by

Anuwak Mathur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Algorithm and Flowchart

Uploaded by

Anuwak Mathur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

Algorithm

 An algorithm is a set of well-defined instructions in sequence to solve a problem.


OR
 An algorithm is a step by step procedure to solve a particular problem.

 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

 Take a cup of water in a vessel


 Add two teaspoons sugar, one teaspoon tea leaves, milk
 Boil for 3 minutes
 Strain into a tea kettle
 Serve in a cup
Algorithm to add two numbers

Add ( a, b )

This algorithm finds the addition of two numbers such as 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 )

This algorithm finds whether a given number n is Even or Odd.

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 )

This algorithm finds greatest number among 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 )

This algorithm finds greatest number among 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 ( )

This algorithm 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 ( )

This algorithm print the table of 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 ( )

This algorithm print the table of 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 )

This algorithm print the table of 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

 A flowchart is a diagram that depicts the “flow” of a program.

 Pictorial representations of steps/algorithm.


Basic Flowchart Symbols

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

Print : “EVEN” Print : “ODD”

End
Flowchart to Find Greatest among two
Numbers
Start

Read : N1, N2

YES NO
If
N1>N2

Print : “N1 is Print : “N2 is


Greatest” Greatest”

End
Flowchart to Find Greatest among three
Numbers
Start

Read : N1, N2, N3

YES If If
NO
N1>N2 N2>N1
and and
N1>N3 N2>N3

Print : “N1 is YES Print : “N3 is


Greatest” Print : “N2 is Greatest”
Greatest”

End
Flowchart to Find Greatest among three Numbers
Start

Read : N1, N2, N3

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

Set i=2 and fact


=1

If
i<= Print : fact STOP
n

Set fact = fact * I


Set i = i+1
Algorithm to find whether a given number
is prime or not
PRIME(n)
This algorithm finds the given number is prime number or not.
Begin
1. Take a value from user, say n
2. set count=0 and i=2
3. Repeat step 4 to 5 until i<=n do
4. if n%i == 0 then
set count = count + 1
5. Set i = i + 1
6. if count == 1 then
print : “Number is Prime”
7. Otherwise
Print: ”Number is not Prime”
End
Flowchart to find whether a given number
is prime or not
START

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

STOP Print : “NOT


PRIME”
Computer Programming Languages

 Machine Language

 Assembly Language

 High Level Language


Machine Language
 Instructions are formed by tacking different combinations of 0s and 1s.

 Advantage:
Translation Free
High Speed

 Disadvantage:
Machine Dependent
Complex Language
Assembly Language

 It uses symbolic notation (Mnemonic) to represent machine language


instructions.

 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

 Convert one language into another language

Program Translator Machine Code

 Assembler
 Compiler
 Interpreter

You might also like