0% found this document useful (0 votes)
6 views23 pages

02-08-2024

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

02-08-2024

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

Algorithm

✔ An algorithm is an effective method expressed as a finite list of well


defined instructions for calculating a function, starting from an initial
state and initial input.
✔ The instructions describe a computation, which will eventually produce
output, when executed.
✔ Algorithm can be used to solve any kind of problems.
✔ However, before writing a program, we need to write the steps to solve
the problem in simple English language. This step-by-step procedure to
solve the problem is called algorithm

1
Algorithm

✔ The way of execution of program is categorized as


✔ Sequence Statements
✔ Selection Statements
✔ Iteration or Looping Statements (Called Control Structure)

2
Algorithm

✔ Sequence Statements:
✔ In this program, all the instructions are executed one after another
✔ Example: Algorithm to print “Welcome to SMEC, VIT Chennai”
Step-1: Start
Step-2: Print “Welcome to SMEC, VIT Chennai
Step-3: Stop

3
Algorithm

✔ Sequence Statements:
✔ In this program, all the instructions are executed one after another
✔ Example: Algorithm to find area of a rectangle”
Step-1: Start
Step-2: Take length and breadth and store them
as L and B
Step-3: Multiply by L and B and store it in Area
Step-4: Print Area
Step-5: Stop

4
Algorithm

✔ Selection Statements ✔ Example: Algorithm to check


✔ In this program, some portion whether a candidate is eligible to
of the program is executed vote?
based upon the conditional Step-1: Start
test. Step-2: Take age and store it in Age
✔ If the conditional test is true, Step-3: Check Age value, if age > 18,
compiler will execute some then go to step-4 else Step-5
part of the program, otherwise Step-4: Print “Eligible to Vote” and go
to Step-6
it will execute the other part of
Step-5: Print “Not Eligible to Vote”
the program.
Step-5: Stop

5
Algorithm

✔ Selection Statements Step-1: Start


✔ Example: Algorithm to check Step-2: Take any number and store it in ‘n’
whether the given number is Step-3: Check ‘n’ value, if n > 0,
+ve, -ve or zero then go to step-5 else Step-4
Step-4: Check ‘n’ value, if n < 0,
then go to step-6 else Step-7
Step-5: Print “Given number is +ve” and
go to Step-8
Step-6: Print “Given number is -ve” and go
to Step-8
Step-7: Print “Given number is Zero”
Step-8: Stop

6
Algorithm

✔ Iterative Statements ✔ Example: Algorithm to print all


✔ In some programs, certain set natural numbers up to ‘n’
of statements are executed Step-1: Start
again and again based upon Step-2: Take any number and store it in ‘n’
conditional test. i.e. executed Step-3: Store 1 in J
more than one time. Step-4: Check ‘J’ value, if J <= n,
then go to step-5 else Step-8
Step-5: Print J
Step-6: Increment J value by 1
Step-7: Go to Step-4
Step-8: Stop

7
Flowchart

✔ Flowchart is a pictorial representation of step by step solution of a


problem
✔ There are various flowchart symbols that carry different messages and
are used for different purposes

Start / Stop Input / Output Processing


or End
parallelogram

Connector
Decision Box
Diamond

Flow of Control
8
Flowchart
✔ Examples:
Simple Interest Calculation Bigger Number among Two
Numbers

P-principal
R-interest rate
T- time duration

9
Flowchart
✔ Examples:
Total Sales Calculation

START A

READ TOTAL SALE =


UNIT PRICE UNITPRICE * QUANTITY

READ PRINT
QUANTITY TOTALSALE

A STOP

10
Flowchart
✔ Examples:
Average of Three Numbers

11
Flowchart
✔ Examples:
Factorial of N

12
Pseudo Code

✔ One of the popular representation of algorithm


✔ It is a kind of structured English for describing algorithms
✔ It allows the designer to focus on the logic of the algorithm without
being distracted by details of language syntax
✔ Pseudocode needs to be complete
✔ It describe the entire logic of the algorithm so that
implementation becomes a rote mechanical task of translating line by
line into source code
✔ Each instruction is written on a separate line

13
Pseudo Code

✔ Keywords and indentation are used to signify particular control


structures
✔ Each set of instructions is written from top to bottom, with only one
entry and one exit
✔ Groups of statements may be formed into modules, and that group given
a name

14
Pseudo Code

Keywords
✔ Several keywords are often used to indicate common input, output, and
processing operations
✔ Input: READ, OBTAIN, GET
✔ Output: PRINT, DISPLAY, SHOW
✔ Compute: COMPUTE, CALCULATE, DETERMINE
✔ Initialize: SET, INIT
✔ Add one: INCREMENT, BUMP

15
Pseudo Code

Example Sequential
✔ Area of Rectangle Calculation

•READ height of rectangle


•READ width of rectangle
•COMPUTE area as height times width
•PRINT area of rectangle

16
Pseudo Code

IF THEN ELSE ENDIF


✔ Format
Example:
IF Condition THEN IF Hours_Worked > Normal_Max THEN
Statements Display overtime message
ELSE ELSE
Statement Display regular time message
ENDIF ENDIF

17
Pseudo Code

WHILE
✔ Format
Example:
WHILE Condition WHILE Population < Limit
Statements Compute Population as Population +
ENDWHILE Births ‐ Deaths
ENDWHILE

18
Pseudo Code

FOR
✔ Format
Example:
FOR Iteration Bounds FOR each student in the list
Statements Print Register_Number
ENDFOR ENDFOR

19
Rules for Variable Names

✔ Names can’t start with a number


✔ There can be no spaces in the name, use underscore _ instead.
✔ Cant use any of these symbols
✔ : <>/?@#$%^&*~-+
✔ Names are lowercase
✔ Avoid using word that have special meaning in python like “list” and
“str”.

20
Exercises

Write the PAC, Algorithm, Pseudo code and draw the Flowchart for the
following cases
✔ Find the area of a circle
✔ Find the area of triangles where three sides are given
✔ Find the area and perimeter of a square
✔ Calculate the average for 3 numbers
✔ Find the greatest of three numbers
✔ Interchange the value of two numbers
✔ Calculate the simple interest
✔ Convert m3 to litre
22
Exercises

Write the PAC, Algorithm, Pseudo code and draw the Flowchart for the
following cases
✔ Find the sum of ‘n’ natural numbers
✔ Find the sum of all even numbers up to ‘n’
✔ Find out whether the given number is even or odd
✔ Find the sum of multiples of 8 up to the given number
✔ Print the numbers from 1 to ‘n’ in reverse order
✔ Find the average marks of 5 subjects of a student
✔ Convert the numeric grade to letter grade

23

You might also like