Introduction to Problem Solving
Precise step-by-step instructions should be given by us to solve the problem. Thus,
problem solving is the process of identifying a problem, developing an algorithm
for the identified problem and finally implementing the algorithm to develop a
computer program.
1: Steps for Problem Solving
1) Analyzing the problem - To clearly understand a problem before we
begin to find the solution for it.
2) Developing an Algorithm It is
essential to device a solution before
writing a program code for a given
problem.
3) Coding - Need to convert the algorithm into the format which can be
understood by
the computer.
4) Testing and Debugging - Testing is a process of finding bugs or errors
in a software product that is done manually by tester or can be
automated. Debugging is a process of fixing the bugs found
in testing phase.
2: Algorithm -
An algorithm is a set of instructions designed to perform a specific task.
3: While writing an algorithm, it is required to clearly identify the
following:
The input to be taken from the user
Processing or computation to be performed to get the desired result
The output
Example 1: Write an algorithm to ride a bicycle.
Solve:
Step 1: Remove the bicycle from the stand,
Step 2: Sit on the seat of the bicycle,
Step 3: Start peddling,
Step 4: Use breaks whenever needed and
Step 5: Stop on reaching the destination.
Example 2: Write an algorithm to find the square of a number.
Solve:
Step 1: Start
Step 2: Input a number , num
Step 3: Square = num * num
Step 4: Print square
Step 5: Stop
4: Flowchart
A flowchart is a type of diagram that represents a workflow or process. A flowchart can also
be defined as a diagrammatic representation of an algorithm, a step-by-step approach to solving
a task. The flowchart shows the steps as boxes of various kinds, and their order by connecting
the boxes with arrows.
Example 1: Make a flowchart to display the sum of two numbers entered by
user.
Solve:
Example 2: Make a flowchart to display the average of five numbers entered
by user.
Solve:
5: Pseudocode
A pseudocode (pronounced Soo-doh-kohd) is another way of representing an
algorithm. It is considered as a non-formal language that helps programmers to
write algorithm. Following are some of the frequently used keywords while writing
pseudocode:
• INPUT
• COMPUTE
• PRINT
• INCREMENT
• DECREMENT
• IF/ELSE
• WHILE
• TRUE/FALSE
Example 1: Write an algorithm to calculate area and perimeter of a rectangle,
using pseudo code.
Solve:
Input length
Input breadth
Compute Area = length * breadth
Compute Perim = 2 * (length + breadth)
Print Area
Print Perim
Example 2: Let us write an algorithm to check whether a number is odd or
even.
Solve:
INPUT number
IF number MOD 2 == 0 THEN
PRINT "Number is Even"
ELSE
PRINT "Number is Odd"
Example 3: Let us write a pseudocode and draw a flowchart where multiple
conditions are checked to categorise a person as based on age specified below:
Child (<13)
Teenager (>=13 but <20)
Adult (>=20)
Solve:
INPUT Age
If Age < 13 then
PRINT "Child"
else if Age < 20 then
PRINT "Teenager"
else
PRINT "Adult"
Exercise__________________________________
1. Write pseudocode that reads two numbers and divide one by another and display the
quotient.
2. Write an algorithm to find the greatest among two different numbers entered by the user.
3. Make a flowchart to calculate area of circle.(Area of circle )
4. Make a flowchart to enter two numbers and display smallest number.
5. Write pseudocode that will marks of three subjects: Computer Science, Mathematics and
Physics, out of 100.
6. Write an algorithm to check inputed number is “single digit” or “double digit” number.
7. Write an algorithm that performs the following: Ask a user to enter a number.
If the number is between 5 and 15, write the word GREEN.
If the number is between 15 and 25, write the word BLUE.
if the number is between 25 and 35, write the wordORANGE.
If it is any other number, write that ALL COLOURS ARE BEAUTIFUL
8: Match the pairs: