You are on page 1of 15

CSE1021-Introduction to Problem Solving and Programming

UNIT-1 CONTENTS (module 2)


 Algorithm
 Examples
 Step for this examples are
 Characteristics of a good algorithm
 Difference between algorithm and program
 Practice question
1. Write an algorithm to add two numbers.
2. Write an algorithm to find the square of a number.
3. Write an algorithm to find GCD of two numbers 45 and 54.
Building block of an algorithm
4. Sequence
5. Selection
Algorithm

 It is defined as sequence of instructions that describe a


method for solving problem. In other words it is a step –by
– step procedure for solving a problem.
 Problem

Algorithm

INPUT COMPUTER OUTPUT


 In our day- to -day life we perform activities by following
certain sequence of steps.
 For Examples:- An activity includes getting ready for
school, making breakfast, riding a bicycle, wearing a tie,
solving a puzzle and so on. To complete each activity, we
follow a sequence of steps. Suppose following are the
steps required for an activity‘riding a bicycle’.
Steps for this examples are:-

Stop on Remove the


reaching the bicycle from
destination. the stand .

Use breaks Sit on the


whenever seat of the
needed bicycle

Start
peddling
Characteristics of a good algorithm

 Precision — the steps are precisely stated or defined.


 Uniqueness — results of each step are uniquely defined and only
depend on the input and the result of the preceding steps.
 Finiteness — the algorithm always stops after a finite number of
steps.
 Input — the algorithm receives some input.
 Output — the algorithm produces some output.
Difference between algorithm and program

S.N0 ALGORITHM PROGRAM

1. Algorithm is finite. Program need not to be finite.

2. Algorithm is written using natural Programs are written using a


language or algorithmic language. specific programming language.
Write an algorithm to add two numbers

 STEP 1:- START


 STEP2:- INPUT TWO NUMBERS NUM1 AND NUM 2
 STEP 3:- ADD NUM1 & NUM2 AND STORE IN RESULTS
 STEP 4:- PRINT RESULT
 STEP 5:- STOP
Q1. Write an algorithm to find area of a square?

Q2. Write an algorithm to find perimeter of rectangle whose


length is 4cm and breadth is 6cm?

Q3. Write an algorithm to find GCD (Greatest Common


Division) of two numbers 45 and 54?

Q4. Find the area of a square if the length of the diagonal is


12cm?

Q5. The length of each side of a square is 5cm and the cost of
painting it is Rs. 5 per sq. cm. Find the total cost to paint the
square?
Q1. Write an algorithm to find area of a square?

 SOLUTION 1:
 Step 1- Start
 Step 2- Input side
 Step 3- Area of square
 Step 4- Print area
 Step 5- Stop
Q3. Write an algorithm to find GCD (Greatest
Common Division) of two numbers 45 and 54?
 Method 1:- Long Division
 GCF of 45 and 54 is the divisor that we get when the
remainder becomes 0 after doing long division repeatedly.
• Step 1: Divide 54 (larger number) by 45 (smaller number).
• Step 2: Since the remainder ≠ 0, we will divide the divisor
of step 1 (45) by the remainder (9).
• Step 3: Repeat this process until the remainder = 0.
 The corresponding divisor (9) is the GCF of 45 and 54.
2 Method of GCD:-
Only for positive
integers

 Step 1: Applying Euclid’s division lemma to a and b we get two


whole numbers q and r such that, a = bq+r ; 0 r < b.
 Step 2: If r = 0, then b is the HCF of a and b. If r ≠0, then apply
Euclid’s division lemma to b and r.
 Step 3: Continue the above process until the remainder is zero.
 Step 4: When the remainder is zero, the divisor at this stage is
the HCF of given numbers.
BUILDING BLOCKS OF ALGORITHM

 It has been proven that any algorithm can be constructed from just three basic building
blocks. These three building blocks are:-
 Sequence
 Selection
 Iteration.

BUILDING BLOCK COMMON NAME


SEQUENCE ACTION
SELECTION DECISION
ITERATION REPETITION OR LOOP
SEQUENCE

 A sequence is one the basic logic structures in computer programming. In a sequence


structure, an action, or event , leads to the next ordered action in a predetermined order.
The sequence can contain any number of actions, but no actions can be skipped in the
sequence. Once running, the program must perform each task without skipping.

 sequence


INSTRUCTION

INSTRUCTION

INSTRUCTION
SELECTION
 A selection ( also called a decision)is also one of the basic logic structures in computer
programming. In a selection structure, a question is asked, and depending on the answer,
the program takes one of two courses of action, after which the program moves on to the
next event.

selection

choice

instruction instruction

You might also like