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

L3-L4 More On Algorithm

Uploaded by

mananbaluja
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

L3-L4 More On Algorithm

Uploaded by

mananbaluja
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Lesson: 3-4

Introduction to algorithms
Objectives

 Introduction to algorithms

 Algorithms for simple problems

2
Algorithm

 An algorithm is a step by step procedure to solve a


particular problem.

 Named after Arabic Mathematician Abu Jafar


Mohammed Ibn Musa Al Khowarizmi

3
Properties of an algorithm

4
Algorithmic Notations
• Name of the algorithm [mandatory]
[gives a meaningful name to the algorithm based on the
problem]

• Step Number [mandatory]


[indicate each individual simple task]

• Explanatory comment [optional]


[gives an explanation for each step, if needed]

• Termination [mandatory]
[tells the end of algorithm]

5
Algorithm to compute the area of circle
Name of the algorithm : Compute the area of a circle

Step1: [Input the value for radius]


Input radius

Step 2: [Compute the area]


Area  3.1416 * radius * radius

Step 3: [Print the Area]


Print ‘Area of a circle =‘, Area

Step 4: [End of algorithm]


Stop

6
Algorithm to compute Simple and
Compound interest
Data:

P=amount deposited
T=duration
R=rate of interest

SI (P*R*T)/100

CI  P*(1+ R/100)T -P

7
Simple and Compound interest
Name of the algorithm: Calculate SI and CI

Step1: [ Read the values of P, T and R]


Input P,R,T

Step 2: [Compute the simple interest]


SI  ( P*R*T)/100

Step 3: [Compute the compound interest]


CI  P*(1+ R/100)T -P

Step 4: [Print the simple and compound interest]


Print ‘Simple Interest=‘,SI
Print ‘Compound Interest=‘,CI

Step 5: [End of Algorithm]


Stop 8
Convert Cel. to Fahren. & vice versa

Algorithm : To convert Celsius to Fahrenheit & vice versa

Step1: [Input celsius]


Read Celsius
Step 2: [Calculate fahrenheit]
Fahrenheit =(9.0 / 5.0) * Celsius + 32.0
Step 3: [Display computed fahrenheit]
Print ‘temp in Fahrenheit=‘ Fahrenheit
Step 4: [Input fahrenheit]
Read Fahrenheit
Step 5: [Calculate Celsius]
Celsius =(Fahrenheit – 32) * (5.0 / 9)
Step 6: [Display computed celsius]
Print ‘temp in celsius=‘ Celsius
Step 7: Stop
9
Interchange values of two variables using a
temporary variable
Name of the algorithm: Interchange of 2 values
Step1: [Read two values]
Input A,B

Step 2: [Store value of variable A in temporary variable temp]


temp  A

Step 3: [Store value of variable B variable A]


AB

Step 4: [Store value of temporary variable temp in variable B]


B  temp

Step 5: [Print the value of variable A and B]


Print ‘A=’ , A
Print ‘B=’ , B

Step 6: [End of Algorithm]


10
Stop
Interchange values of two variables without
using a temporary variable
Name of the algorithm: Interchange two values without using a
temporary variable
Step 1: [Read two values]
Input A,B

Step 2: [Assign sum of A and B to A]


AA+B

Step 3: [Assign difference of A and B to B]


BA-B

Step 4: [Assign difference of A and B to a]


AA-B

Step 5: [Print the value of variable A and B]


Print ‘A=’ , A
Print ‘B=’ , B
Step 6: [End of Algorithm]
11
Stop
Sum of digits of a 4-digit number
Name of the algorithm: Sum of digits of 4-digit number
Step1: [Read the number]
Input N

Step 2: digit1N Mod 10


NN/10 (integer)
Step 3: digit2N Mod 10
NN/10 (integer)
Step 4: digit3N Mod 10
NN/10 (integer)
Step 5: digit4N Mod 10
NN/10 (integer)

Step 6: sum digit1+digit2+digit3+digit4

Step 7: Print “Sum of digits = “, sum

Step 8: [End of Algorithm]


12
Stop
Largest of 3 Numbers
Name of the algorithm: Find largest of 3 numbers

Step 1: [Read the values of A, B and C]


Read A, B, C

Step 2: [Compare A and B]


IF A>B go to step 4

Step 3: [Otherwise compare B with C]


IF B>C then
Print ‘B’ is largest’
Else
Print ‘C’ is largest’
Go to Step 5

Step 4: [Compare A and C for largest]


IF A>C then
Print ‘A’ is largest’
Else
Print ‘C’ is largest’

Step 5: [End of the algorithm] 13


Sum of first N natural numbers
Name of the algorithm: Sum of natural numbers.

Step1: [Read the value of N]


Input N

Step 2: [Set Sum equal to 0]


Sum  0

Step 3: [Compute the sum of all first N natural numbers]


For i=1 to N in step of 1 do
begin
Sum  Sum + i
end

Step 5: [Print Sum ]


Print ‘Sum of N natural numbers=‘,Sum

Step6: [End of algorithm]


Stop 14
Factorial of a number
Name of the algorithm: Compute the factorial of a number N

Step1: [Read the value of N]


Input N
Step 2: [Set variable Fact equal to 1]
Fact 1
Step 3: [Compute the factorial of N]
for Count = 1 to N in step of 1 do
begin
fact  fact*Count
end
Step 4: [Print Fact ]
Print ‘fact of N=‘, Fact

Step 5: [End of algorithm]


Stop

15
Sum of digits of a number
Name of the algorithm: Find sum of the digits of a number
Step 1: [Read the number]
Input N
Step 2: [Set variable Sum equal to 0]
Sum0
Step 3: [Compute the sum of digits]
While N>0
begin
rem  N mod 10
sum  sum + rem
NN/10 (integer)
end

Step 4: [Print the sum of digits]


Print ‘Sum of the digits of the number=‘,sum
Step 5: : [End of Algorithm] 18
Summary

 Properties of an algorithm
 Algorithms for simple problems

19

You might also like