ALevel 1 Python 30mar SS
ALevel 1 Python 30mar SS
O Level / A Level
Type of Algorithms
The algorithm and flowchart, classification to the three types of control structures.
They are: 1. Sequence 2. Branching (Selection) 3. Loop (Repetition)
These three control structures are sufficient for all purposes. The sequence is exemplified by
sequence of statements place one after the other – the one above or before another gets executed
first. In flowcharts, sequence of statements is usually contained in the rectangular process box.
Branching
The branch refers to a binary decision based on some condition. If the condition is true, one of
the two branches is explored; if the condition is false, the other alternative is taken. This is
usually represented by the ‘if-then’ construct in pseudo-codes and programs. In flowcharts, this
is represented by the diamond-shaped decision box. This structure is also known as the selection
structure.
Problem 1: write algorithm to find the greater number between two numbers
Step1: Start
Step2: Read/input A and B
Step3: If A greater than B then C=A
Step4: if B greater than A then C=B
Step5: Print C
Step6: End
Problem 2: write algorithm to find the result of equation: f(x) ={ -x, x < 0 ,
x, x>=0 }
Step1: Start
Step2: Read/input x
Step3: If X Less than zero then F=-X
Step4: if X greater than or equal zero then F=X
Step5: Print F
Step6: End
Loops
The loop allows a statement or a sequence of statements to be repeatedly executed
based on some loop condition. It is represented by the ‘while’ and ‘for’ constructs in
most programming languages, for unbounded loops and bounded loops respectively.
(Unbounded loops refer to those whose number of iterations depends on the
eventuality that the termination condition is satisfied; bounded loops refer to those
whose number of iterations is known before-hand.) In the flowcharts, a back arrow
hints the presence of a loop. A trip around the loop is known as iteration. You must
ensure that the condition for the termination of the looping must be satisfied after
some finite number of iterations, otherwise it ends up as an infinite loop, a common
mistake made by inexperienced programmers. The loop is also known as the
repetition structure.
Examples:
Problem 1: An algorithm to calculate even numbers between 0 and 99
1. Start
2. I ← 0
3. Write I in standard output
4. I ← I+2
5. If (I <=98) then go to line 3
6. End
Problem 2: Design an algorithm which gets a natural value, n,as its input and
calculates odd numbers equal or less than n. Then write them in the standard output:
1. Start
2. Read n
3. I ← 1
4. Write I
5. I ← I + 2
6. If ( I <= n) then go to line 4
7. End
Problem : Design an algorithm which generates even numbers between 1000 and 2000
and then prints them in the standard output. It should also print total sum:
1. Start
2. I ← 1000 and S ← 0
3. Write I
4. S ← S + I
5. I ← I + 2
6. If (I <= 2000) then go to line 3 else go to line 7
7. Write S
8. End
Problem 4: Design an algorithm with a natural number, n, as its input which calculates
the following formula and writes the result in the standard output:
S = ½ + ¼ + … +1/n
1. Start
2. Read n
3. I ← 2 and S ← 0
4. S= S + 1/I
5. I ← I + 2
6. If (I <= n) then go to line 4 else write S in standard output
7. End
Step 1: Start
Step 2: Read two number for which GCD is to be found , say (A, B)
Step 3: Let A be the larger of the two numbers.
Step 4: Divide A by B.
Step 5: Get the remainder of the division operation.
Step 6: Divide the divisor of the previous division operation with the remainder.
Step 7: Repeat steps 4 , 5 ,6 till the remainder become zero.
Step 8: The divisor of the last division operation performed is the GCD of the two
numbers.
Step 9: Stop
Assignments