Lecture 2
Lecture 2
Algorithm
Start
peddling
Characteristics of a good algorithm
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
It has been proven that any algorithm can be constructed from just three basic building
blocks. These three building blocks are:-
Sequence
Selection
Iteration.
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