0% found this document useful (0 votes)
21 views7 pages

Problem Solving

Uploaded by

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

Problem Solving

Uploaded by

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

CHAPTER – 4 (INTRODUCTION TO PROBLEM SOLVING)

INTRODUCTION : Computers are used for solving various day-to-day problems and thus problem
solving is an essential skill that a computer science student should know. 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.

STEPS FOR PROBLEM SOLVING :

Step 1: Identify the problem Step 2: Create an algorithm

Step 3: Make a code Step 3: Testing and debugging

Step 4: Problem is solved

Identify the problem : First of all we have to identify the problem before we begin to find the
solution. After that decide the core functionalities that our solution should have.

Create an algorithm : The solution is represented in natural language and is called an algorithm.
Or we can say A plan which can solve the problem step by step in a systematic order is known as
algorithm.

Make a code : After finalising the algorithm, we need to convert the algorithm into the format
which can be understood by the computer to generate the desired solution.

Testing and Debugging : The program created should be tested on various parameters. The
program should meet the requirements of the user. It should generate correct output for all
possible inputs. In the presence of syntactical errors, no output will be obtained. In case the
output generated is incorrect, then the program should be checked for logical errors, if any.

ALGORITHM : Algorithm is a set of well-defined instructions to solve a particular problem. It takes


a set of input(s) and produces the desired output. For example, In our day-to-day life we perform
activities by following certain sequence of steps.
Examples of activities include getting ready for school, making breakfast, riding a bicycle, wearing
a tie, solving a puzzle and so on.
Another example :
An algorithm to add two numbers:
1. Take two number inputs
2. Add numbers using the + operator
3. Display the result

Need of Algorithm :
1
1. To understand the basic idea of the problem.

2. To find an approach to solve the problem.

3. To improve the efficiency of existing techniques.

4. To understand the basic principles of designing the algorithms.

5. A good design can produce a good solution.

6. To understand the flow of the problem

(A) Characteristics of a good algorithm

(1) Uniqueness — results of each step are uniquely defined and only depend on the input and the
result of the preceding steps.

(2) Input and output should be defined precisely.

(3) Precision — the steps are precisely stated or defined.

Representation of Algorithms
A flowchart is a visual representation of an algorithm. A flowchart is a diagram made up of boxes,
diamonds and other shapes, connected by arrows. Each shape represents a step of the solution
process and the arrow represents the order or link among the steps.

2
Example 1 : Write an algorithm to add two numbers entered by user.

Step 1: Start
Step 2: Declare variables num1, num2 and sum.
Step 3: Read values num1 and num2.
Step 4: Add num1 and num2 and assign the result to sum.
sum←num1+num2
Step 5: Display sum
Step 6: Stop

Diagramatic Representation

Pseudocode : A pseudocode (pronounced Soo-doh-kohd) is another way of representing an


algorithm. It is a detailed description of instructions that a computer must follow in a particular
order. The word “pseudo” means “not real,” so “pseudocode” means “not real code”. Following
are some of the frequently used keywords while writing pseudocode:

• INPUT • COMPUTE • PRINT • INCREMENT

• DECREMENT • IF/ELSE • WHILE • TRUE/FALSE

Online Link : https://pseudocode.deepjain.com/

3
Example : Write an algorithm to display the sum of two numbers entered by user, using
pseudocode

Pseudocode for the sum of two numbers will be:


INPUT num1
INPUT num2
COMPUTE Result = num1 + num2
PRINT Result

Example : Write an algorithm to calculate area and perimeter of a rectangle, using both
pseudocode and flowchart

Pseudocode
INPUT length
INPUT breadth
COMPUTE Area = length * breadth
PRINT Area
COMPUTE Perimeter = 2 * (length + breadth)
PRINT Perimeter

4
Benefits of Pseudocode : pseudocode helps them to review the steps to confirm that the
proposed implementation is going to achieve the desire output.

FLOW OF CONTROL : The events can flow in a sequence, is called flow of control.

1 . Sequence : Such algorithms where all the steps are executed one after the other are said to
execute in sequence.

2. Selection : we can understand selection with the help of example.


Example : Checking eligibility for voting.
Depending on their age, a person will either be allowed to vote or not allowed to vote:
• If age is greater than or equal to 18, the person is eligible to vote
• If age is less than 18, the person is not eligible to vote
As we see on the above example if the candidate complete his/her age greater than or equal to
18 than we can allowed him/her to give vote, this is known as selection.

Conditional statements : Statements which work according to the condition or requirement is


known as conditional statement.
Example : if - then - else statement.

If <condition> is true then


Right statement executed
Else
False statement executed

Example : Write a code to check first number is greater than second number or not.

Code : firstnumber = int(input(“Enter first number”))


Secondnumber = int(input(“Enter second number”))

If ( firstnumber > Secondnumber ) :


Print(“Firstnumber is greater”)
else :
Print(“Second number is greater )

5
Example : Write a code to check the student is Child, Teenager or Adult.

Code : EnterAge = int(input("Enter Age "))


if(EnterAge < 13):
print("Child")
elif(EnterAge < 20):
print("Teenager")
else:
print("Adult")

Repetition: Repetition statements are called loops, and are used to repeat the same code
multiple times in succession . Python has two types of loops

(1) For loop (2) while loop

(1) For loop : The for loop as a Count-Controlled loop iterates a specific number of times. Before
we start first of all we have to understand the concept of Range.

Example : Print the number from 1 to 5

Code : for i in range(1,10,1)


print(i)

output 1
2
3
4
5
Example : Print the number from 5 to 1

Code : for i in range(5,1,-1)


print(i)

output 5
4
3
2
1

6
7

You might also like