Algorithms and
Flowcharts
T.A. Asmaa Hamad El-saied
DECISION
STRUCTURES
NESTED IFS
✗ One of the alternatives within an IF–THEN–ELSE
statement may involve further IF–THEN–ELSE
statement
Example 1
✗ Write an algorithm that reads three numbers and
prints the value of the largest number.
Example 1
Step 1: Start
Step 2: Input N1, N2, N3
Step 3: if (N1>N2) then
if (N1>N3) then
MAX N1 [N1>N2, N1>N3]
else
MAX N3 [N3>N1>N2]
endif
else
if (N2>N3) then
MAX N2 [N2>N1, N2>N3]
else
MAX N3 [N3>N2>N1]
endif
endif
Step 4: Print “The largest number is”, MAX
Step 5: End
Loop
(Repetition)
Loop (Repetition): Examples
✗ Ex1 : Design an algorithm with a natural number, n, as its input
which calculates the following formula and writes the result in
the standard output:
𝟏 𝟏 𝟏
𝑺= + + ⋯+
𝟐 𝟒 𝒏
Step 1: Start
Step 2: Input n
Step 3: I ← 2 and S ← 0
Step 4: S ← S + 1/I
Step 5: I ← I + 2
Step 6: If (I <= n) then go to Step 4
else
print S
Step 7: End
Loop (Repetition): Examples
𝟏 𝟏 𝟏
✗ Ex1: 𝑺 = + + ⋯+
𝟐 𝟒 𝒏 START
Flowchart Input n
I2,S0
S S+1/I
I I+2
YES Is
I<=N
NO
Print S
End
Loop (Repetition): Examples
Ex2: Write an algorithm and draw a flow chart to
calculate x^n.
Step 1: Start
Step 2: Input x , n
Step 3: result x
Step 4: Counter 1
Step 5: if (Counter < n) then
result result* x
Counter Counter + 1
go to step 4
Step 6: Print result
Step 7: End
Loop (Repetition): Examples
START
Flowchart
Input x, n
result x, counter 1
Is NO
counter<n
YES
Print result
result result *x
counter counter+1 End
Loop (Repetition): Examples
✗ Ex3: Write an algorithm and draw a flow chart to
calculate the factorial of a number (N).
Step 1: Start
Step 2: Input N
Step 3: fact 1
Step 4: Counter 1
Step 5: if (Counter <= n) then
fact fact* counter
Counter Counter + 1
go to step 4
Step 6: Print fact
Step 7: End
Loop (Repetition): Examples
START
Flowchart
Input N
fact 1, counter 1
Is NO
counter<=n
YES
Print fact
fact fact*counter
counter counter+1 End
Loop (Repetition): Examples
Ex4: Write an algorithm to generate(print) first 50
items of the Fibonacci series:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, …?
Is a series in which, the next number is found by adding up the two
numbers before it.
The 2 is found by adding the two numbers before it (1+1)
The 3 is found by adding the two numbers before it (1+2),
And the 5 is (2+3),
and so on!
Loop (Repetition): Examples
An algorithm 0,1, 1, 2, 3, 5, 8,…?
Step 1: Start
Step 2: a 0, b 1, i 1
Step 3: print a , b
Step 4: S a+b
Step 5: print S
Step 6: a b
Step 7: b s
Step 8: i i+1
Step 9: if (i<= 50) then
go to step 3
Step 10: End
Exercises
Write an algorithm and draw a flowchart to enter a number and
displays whether the number is positive or negative .
Write an algorithm and draw a flowchart to generate odd numbers
between 1000 and 2000 and then prints them in the standard output. It
should also print total sum.
Write an algorithm and draw a flowchart for the problem of
determining prime number?
Write a Algorithm that computes the first 20 terms of the expression:
1 3 5
+ + +⋯
𝑛 𝑛2 𝑛3
Thanks!
16