Problem Solving
ALGORITHMS AND FLOWCHARTS
Introduction - Algorithms
We can consider algorithms to be practical
solutions to problems. These solutions are not
answers, but specific instructions for getting
answers.
Programming as a Problem Solving Process
A typical programming task can be divided into
two phases:
Define and analyze the problem.
What is the input & output?
Develop an algorithm.
What steps must be done?
Implement a program.
in programing languages
Compile, test, and debug the program.
Maintain the program.
Use and modify the program if the problem domain
changes
Programming as a Problem Solving Process
A set of step-by-step instructions to accomplish a
task.
An algorithm must have start instruction
Each instruction must be precise/ to the point.
Each instruction must be unambiguous.
Each instruction must be executed in finite time.
An algorithm must have stop instruction.
Representation of Algorithms
As programs
As flowcharts
As pseudocodes
When an algorithm is represented in the form of a
programming language, it becomes a program
Thus, any program is an algorithm, although the
reverse is not true
Expressing Algorithms
English description
More easily More
expressed Pseudo-code precise
High-level
programming language
PSEUDOCODE & ALGORITHM
• SEQUENTIAL LOGIC
STRUCTURE
• Sequential logic structure – we
ask the computer to process a set
of instructions in sequence from
the top to bottom of an algorithm
OR
• a sequence followed without
skipping any line
PSEUDOCODE & ALGORITHM
Example 1: Write a sequential algorithm to prepare a
cup of tea.
PSEUDOCODE & ALGORITHM
Write algorithm for given problem:
You have a bottle of Pepsi and 7up. You have to swap the
liquid of bottle (i.e. in bottle of 7up you will have Pepsi and
in bottle of Pepsi you will have 7up).
PSEUDOCODE & ALGORITHM
Example 3: Write an algorithm to convert the
length in feet to centimeter.
Algorithm:
Input the length in feet (Lft)
Calculate the length in cm (Lcm) by multiplying LFT
with 30
Print length in cm (LCM)
EXAMPLE 3
Pseudocode
Step 1: Start
Step 2: Input Lft
Step 3: Lcm = Lft x 30
Step 4: Print Lcm
Step 5: End
PSEUDOCODE & ALGORITHM
Write an pseudo code that displays five number with their
squares
PSEUDOCODE & ALGORITHM
Quadratic equation= ax2+bx+c=0
PSEUDOCODE & ALGORITHM
Write an pseudo code that input value of a, b and c from
the user and compute the value of x by using quadratic
equation.
DECISION STRUCTURES
Conditional/Decision programing – a structure which requires a
decision for any lines of code to be executed.
Y Is N
condition
true
Process 1 Process 2
Decision Structure: Pseudocode
IF–THEN–ELSE STRUCTURE
The structure is as follows:
IF condition is TRUE THEN
Process step(s) 1
ELSE
Process step(s) 2
END IF
IF–THEN–ELSE STRUCTURE
The algorithm for the flowchart is as follows:
If A>B then
print A
else
Y N
print B is
A>B
endif
Print Print
Print
AA PrintBB
PSEUDOCODE & ALGORITHM
Read a number from user and check if it is even or not
PSEUDOCODE & ALGORITHM
Find smallest of 3 numbers