You are on page 1of 22

Algorithms and Flowcharts

Algorithm
• Set of instructions
• Series of steps necessary to find the solution to a problem
• Also called procedure or a function used to perform certain task.
• Properties of a good Algorithm
• Finiteness- must terminate after a finite number of steps
• Non-ambiguity- each step must be precisely defined
• Effectiveness- all operations performed can be carried out in a finite time
Statements required to describe algorithms

• Statements are executed in the written order (sequence)


• Grouping of several statements together – to handle as if they were one statement
( compound statement)
• Allowing repetition of a statements or compound statements (repetition/looping)
• Allowing selection of next statement depending on the value of a condition
(selection)
• Ability to assign a value to a quantity (assignment)
Algorithm : add two numbers

Step 1: Start
Step 2: Read A and B
Step 3: C=A+B
Step 4: Print C
Step 5: Stop
Find the sum of first 10 numbers
Step 1: Start
Step 2: Let num =0, sum=0
Step 3: sum=sum+num
Step 4: is num>10, if true then Step 7 else Step 5
Step 5: num=num+1
Step 6: Repeat Step 3 and 4
Step 7: Print sum
Step 8: Stop
Algorithm to find the factorial of a number
Step 1 : Start
Start 2 : Read n
Start 3 : Initialize counter variable i to 1 and fact to 1
Start 4 : if i <= n go to step 5 otherwise goto step 7
Start 5 : calculate fact = fact * i
Start 6 : increment counter variable i and goto step 4
Start 7 : Print fact.
Start 8 : Stop
Flowchart

• Pictorial representation
• Shows the steps taken to reach a certain result
• Drawn using certain special-purpose symbols ( rectangles, diamonds, ovals)
• Symbols are connected using flow lines.
Flowchart Symbols
Terminal Box (oval shape) Input/Output (Parallelogram)
• Used to mark the beginning and • For showing the output and to get the
end of the flowchart input
• In the beginning, START is written • For showing input- Input/Read is
in the box and in the end STOP. written inside the symbol
• For showing the output- Print is written
START
PRINT name
Process Box( Rectangular Shape) Decision Box( Rhombus/diamond Shape)
• Shows the processing point • Used when condition is to be checked
• Used to show various computations • Only two answers- yes / no
or calculations • Has two exit points and one entry point

A=A+1

No

Yes
Flow Lines (Direction Arrows) Connectors (Round Shape)
• Symbols are connected to each other • Used to show a jump from one point
by using arrows (to represent the in the process flow to another.
sequence/flow) • Connectors are usually labeled with
• Four directional arrows- up, down, capital letters (A, B, AA) to show
left , right matching jump points. They are
handy for avoiding flow lines that
cross other shapes and flow lines.

A
Draw a flowchart to find the sum of two numbers.
START

READ A,B

C=A+B

PRINT C

STOP
START
Find the greatest of two numbers

READ A,B
Step 1: Start
Step 2: Read A and B
Step 3: Is A>B No Is
Yes
A>B
if Yes go to step 4 ?
else go to step 6
Step 4: Print “A is greater” Print “B is greater” Print “A is greater”
Step 5: Go to step 7
Step 6: Print “ B is greater”
Step 7: Stop
STOP
START

Factorial of a given number READ N

fact=1, i=1

fact=fact x i

Is Yes
No
i<=N i=i+1
?
PRINT fact

STOP
Assessment
• Write an algorithm and draw a flow chart to
1. Find the sum of first 10 natural numbers
2. Find whether a number is even or odd
3. For calculating the conversion from rupees to dollars
4. Find the sum of all even numbers between 1 and 20
5. Check whether a number is divisible by 5 or not.
6. Find the greatest among three numbers.
Python
• Developed by Guido Van Rossum
• Interpreted language
• Easy to use
• Portable language/ cross platform language
• Free & Open source
Python Fundamentals
• Python Character Set: Set of valid characters that Python language can recognize
• Character represents any letter, digit or any other sign
• Letters A- Z, a- z
• Digits 0- 9
• Special Symbols : Space + - * / ^ \ ( ) { } [ ] = != < > . ‘ “ ’’’ $ , : ; % ! ? &
_(underscore) # <= >= @ >>> << >> //
• White spaces : Blank Space, Horizontal tab, Carriage return, Newline, form feed
• Other characters: all ASCII and Unicode characters
Tokens
• The smallest individual unit in a program
Keywords
Identifiers (Names)
Literals
Punctuators
Operators
Keywords:
• convey a special meaning to the language compiler/interpreter.
• Reserved for special purpose
• Must not be used as identifier names
• Eg: False, from, for, continue,try,return
Identifiers:
• Fundamental building blocks of a program
• Names given to different parts of a program
• Arbitrarily long sequence of letters and digits
• First character must be a letter (_ is counted as a letter)
• Upper and lower case are different
• Names cannot start with a digit, but can contain digits
• Unlimited in length
• Must not be a keyword in Python
• Cannot contain any special character other than (_)underscore
Literals/Values
• Referred to as constants (data items that have fixed value)
Different types

• String literals – text enclosed in quotes (‘a’, ‘abc’, “xy”, “r”)


• Numeric literals – int , long, float, complex
• Boolean literals – used to represent values True or False
• Special literal None – used to indicate something that has not been created.

String literals can be formed by enclosing text in single quotes or double quotes.
String Literals
• Sequence of characters surrounded by quotes
• Python allows non-graphic characters (represented using escape sequences )
• Non-graphic characters are not printed, instead a corresponding action takes
place. (escape sequence is represented using \)
• Eg: \n: newline, \\(backslash), \t tab
• Python allows two string types:
• Single line strings
• Multiline strings
Single line strings (basic strings)
• Strings created by enclosing text in single quotes or double quotes
• Basic string must be completed on a single line

Multiline Strings
• Text spread across multiple lines as one single string
• By adding a backslash at the end of a normal string
Text1=‘hello\
World’
• By typing the text in triple quotation marks
Str= ‘“Hello
world ”’
Numeric Literals
• Integer literals
• Floating point literals
Boolean Literals

You might also like