UMBB, Faculty of Science, Department of Computer Science
ING,LI, First year, Semester 1, Algorithms and data structures 1
Tutorial 2: The loops
Lecture notes :
The three repetitive instructions are:
While <condition> do <action> endWhile;
For <counter> ← <initialVal> to <finalVal> do <action> endFor ;
Repeat <action> until <condition> ;
The block delimiters ftq and fpour are mandatory even if there is only one instruction.
<counter> is an integer variable. It cannot be modified within the repetitive block. <finalValue> is an
expression that evaluates to an integer or a real number.
It is assumed that there are no predefined operators (or functions) for modulo, integer division, or
exponentiation.
Exercise 1: Exercise 6:
Algorithm SumNumbers; A number is said to be prime if it has no divisors other
than 1 and itself.
Var N, nbr, S, I : integer; Write an algorithm that checks if a number is prime.
Begin Exercise 7:
write('Enter the number of elements'); Write an algorithm that determines the GCD (Greatest
read(N); Common Divisor) of two numbers.
For ( I ← 1 to N ) Do Exercise 8:
write('Enter your elements:'); Write an algorithm that evaluates the series 𝑆𝑛 for a
read(nbr); given n :
endFor ;
For I ← 1 to N Do
S ← 0; Exercise 9:
S ← S + nbr; Construct an algorithm that, for a four-digit number N ,
write('The sum of your elements is:', S); swaps its first digit with the last and the second digit
endFor with the third.
1. Execute this algorithm for the values N = 4 and Ex: 1534 4351
elements: 10, 15, 22, 8; then deduce that its
solution is incorrect. Exercise 10:
2. In your opinion, what flawed reasoning led this Let the following algorithms be:
beginner to make these mistakes? Algorithm version1;
Exercise 2 Var N,M,I,J,comp:integer ;
Write an algorithm that determines the sum of the first N Begin
positive integers. Read(N, M);
For example, if N=4, then the sum is 1+2+3+4=10. comp ← 1;
I ← 1;
Exercise 3 While (I ≤ N) Do
Write an algorithm that determines the n-th power of a J ← 1;
given number a (where n is a non-negative integer), i.e., While (J ≤ M) Do
𝑎𝑛 . write(I, J, comp);
Exercise 4: comp ← comp + 1;
Write an algorithm that determines the remainder of the J ← J + 1;
division of an integer A by an integer B. End While
I ← I + 1;
Exercise 5: End While
Write an algorithm that checks if a given number is End.
divisible by 2.
Algorithm version2 ;
Var N,M,I,J,comp: integer ; Algorithm Bizarre2
Begin Var A, S, Counter : integer;
read(N,M) ;comp←1; I←1 ; J←1 ; Begin
Read(A);
While (I ≤ N) et (J ≤ M) do S ← 0;
write(I,J,comp); Counter ← 1;
comp ← comp+1 ; While (Counter ≤ A) Do
J ←J+1; S ← S + Counter;
I ← I+1; Counter ← Counter + 1;
endWhile; Write(Counter);
End.
EndWhile;
Write(S);
1. Unroll the previous algorithms for the values N = 3
End.
and M = 4 .
2. Is there a difference in what they display? Justify your
Unroll both algorithms above. What can be concluded?
answer.
Additional Exercises
Exercise 13:
Exercise 11 :
Write an algorithm that determines the n-th power of a
Algorithme Inconnu ;
given number a (where n is any integer), i.e.𝑎𝑛 .
Var A,B,S,I : integer ;
Begin
Exercise 14:
read(A,B) ; S ←A ;
Write an algorithm that determines the quotient of the
I ←1 ;
division of an integer A by an integer B.
While (≤I B ) Do
S ←S+I ; Exercise 15:
I ←I+1 ; A number is said to be perfect if it is equal to the sum of
endWhile ;
its divisors, excluding the number itself.
write(S) ;
Example: Six is a perfect number because
End.
6 = 1 + 2 + 3. Write an algorithm that checks if a number
1. Unroll this algorithm with the values A = 2 and B = 4,
is perfect.
A=4 et B = 2 ?
Exercise 12: Exercise 16 :
Algorithm Bizarre1; Write an algorithm to evaluate a polynomial( P(x) of
degree n for a given value of x .
Var A, S, Counter : integer;
Begin Exercise 17 :
Read(A); Let N be a five-digit integer. Write an algorithm that
S ← 0; displays the middle digit and the sum of the other digits.
Counter ← 1;
While (Counter ≤ A) Do Exercise 18:
S ← S + Counter; Let N be a three-digit integer. Write an algorithm to
Write(Counter); calculate the sum of the cubes of its digits.
EndWhile;
Write(S);