Professional Documents
Culture Documents
Chapter 2
Chapter 2
Example:
Design an algorithm that adds two numbers
and display the result
Solution
1. Start.
2. Input radius in cm.
3. Calculate, area = 3.14 * (radius * radius)
4. Display area.
5. Stop
Example 1
Design an algorithm which calculates a
quadratic equation
Ax2 + BX + C = 0
MTU, Electrical & Computer Engineering 2012 9
solution
Step 1: Read the value of A, B and C
and store them in the memory.
Step 2: If A = 0 and B = 0 then print
"No root exists" and stop. Else continue.
Step 3: If A = 0 and B is not equal to 0,
then r1 = - C/B and output the
root. Else continue.
Step 4: Compute D = (B2- 4AC)
Step 5: If D = 0, then compute
r1 = r2 =( -B/2A), output r1 and r2 .
Else continue.
MTU, Electrical & Computer Engineering 2012 10
Step 6: If D < 0, then write "Roots are
not real" and stop. Else continue.
Step 7: If D > 0, then calculate
r1 = ( -B + (B2- 4AC)/(2A))
r2 = ( -B -(B2- 4AC)/(2A)) and
display r1 and r2
1. Start.
2. Accept five different subjects’ marks, i.e.
m1, m2, m3, m4, m5.
3. Calculate average ,
AV = (m1+m2+m3+m4+m5) / 5
4. If AV >= 85 then display “Excellent”
elseIf AV >= 75 then display “Very good”
elseIf AV >= 60 then display “Good”
elseIf AV >= 40 then display “Satisfactory”
else Display “Fail”.
5. Stop.
MTU, Electrical & Computer Engineering 2012 12
Iteration
Used when one or more instructions
may be executed several times
depending on some condition.
Single instructions used repeatedly
Pseudo code
BEGIN
READ balance, rate, name
Interest=balance*rate
WRITE name, interest
END
Pseudo code
BEGIN
READ X, Y, Z
S=X+Y+Z
A=S/3
P=X*Y*Z
WRITE S, A, P
END