0% found this document useful (0 votes)
90 views

Properties: - A Step by Step Procedure To Solve A Problem

The document discusses algorithms including their definition, properties, qualities, steps to develop one, different types (sequential, selectional/decision, iterative/repetitive, recursive), and provides examples of algorithms to find the average of marks, determine if a number is largest of 3, print the first 10 natural numbers, and calculate a factorial recursively.

Uploaded by

Divyansh Rai
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
90 views

Properties: - A Step by Step Procedure To Solve A Problem

The document discusses algorithms including their definition, properties, qualities, steps to develop one, different types (sequential, selectional/decision, iterative/repetitive, recursive), and provides examples of algorithms to find the average of marks, determine if a number is largest of 3, print the first 10 natural numbers, and calculate a factorial recursively.

Uploaded by

Divyansh Rai
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 18

Algorithm

• A step by step procedure to solve a problem


• Properties
a. Finite number of steps
b. Each step shall be explicit and unambiguous
c. Should not be repeated
d. Should be written in simple English

Qualities of Good Algorithm


a. Time
b. Memory
c. Accuracy
Steps to Develop an Algorithm
Properties of an Algorithm
Different patterns in Algorithm
Building blocks of Algorithm
• Algorithm employ 3 control structures
– Sequence
– Decision
– Iteration or Repetition
– Recursive
Sequential Algorithms
• Each step of the algorithm is executed in
specified order
Statement 1

Statement 2

Statement n
Algorithm for adding two numbers
Step 1: Read two numbers A and B
Step 2: Let C = A + B
Step 3: Display C
Area of a Circle
Step 1 : Read the RADIUS of a circle
Step 2 : Find the square of RADIUS and store it
in SQUARE
Step 3 : Multiply SQUARE with 3.14 and store
the result in AREA
Step 4: Print AREA
Average Marks
• Find the average marks scored by a student in
3 subjects:
Step 1 : Read Marks1, Marks2, Marks3
Step 2 : Sum = Marks1 + Marks2 + Marks3
Step 3 : Average = Sum / 3
Step 4 : Display Average
Selectional/Decision Algorithms
• It is used when the outcome of the process
depends on some condition

True False
Number Equal
Step 1: Start
Step 2: Input first number as A
Step 3: Input Second number as B
Step 4: If A==B
Print “Equal”
Else
Print “Not Equal”
Step 5: Stop
Algorithm to find largest among 3
different numbers entered by User
Step 1: Start
Step 2: Declare variables a, b and C
Step 3: Read Variables a, b and C
Step 4: If a>b
If a>c
Display a is the largest number.
Else
Display c is the largest number
If b>c
Display b is the largest number
Else
Display c is the largest number
Step 5: Stop
Pass/ Fail and Average
• Write an algorithm to find the average marks of a
student (three subjects). Also check whether the
student has passed or failed. For a student to be
declared pass, average marks should not be less
than 65.
Step 1 : Read Marks1, Marks2, Marks3
Step 2 : Total = Marks1 + Marks2 + Marks3
Step 3 : Average = Total / 3
Step 4 : Set Output = “Student Passed”
Step 5 : if Average < 65 then Set Output =
“Student Failed"
Step 6 : Display Output
Iterative or Repetition
• Executing one or more steps for a no of times using while
loops and for loops
• 2 ways of testing
pre-test Loop-condition is tested at the beginning of
the loop
post-test Loop-Test the condition at the end of the loop
Average marks scored by ‘N’ students
Step 1 : Accept Number Of Students
Step 2 : Count = 1
Step 3 : Read Marks1, Marks2, Marks3
Step 4 : Total = Marks1 + Marks2 + Marks3
Step 5 : Average = Total / 3
Step 6 : Set Output = “Student Passed”
Step 7 : If (Average < 65) then Set Output = “Student Failed"
Step 8 : Display Output
Step 9 : Count = Counter + 1
Step 10 : If (Counter <= Number of Students ) then go to step 3
Algorithm that print 1st 10 natural
number
• Step1:Start
• Step2:Set I=1,N=10
• Step3:Repeat step 3 and 4 while I<=N
• Step 4:print I
• Step 5:Set I=I+1
• Step 6:End
Recursive
• It involves a function calling itself until
specified condition is met
Recursive algorithm to find the
factorial of a number
step 1:start
step 2:Input number as n
step3:call factorial(n)
step 4:End
Factorial(n)
Step1:set f=1
Step 2:If n==1 then return 1
Else
set f=n*factorial(n-1)
Step 3: print f

You might also like