Saad Dahlab University blida 1 1st Year / MI
Faculty of Sciences DW N°4 : Iterative Structures Algo1
Exercise 1 : (Course)
• Translate the following loops “For" in loops “While“ and “Repeat … Until”, and give
the display. (We put x = 10 in the first loop).
For i0 to 50 step x do For j30 to 5 step -5 do For i1 to 3 step 1 do
Print(i) ; Print(j) ; For j 1 to 2 step 1 do
EndFor; EndFor; Print(“i=“, i, “ j=“, j);
EndFor;
EndFor;
Exercise 2 : (Course)
Write in algorithmic language a treatment for :
1- Display the sum of integer numbers that are between 1 and 100.
2- Display the even integers which are between 1 and 80 (then the odd numbers).
3- Display all natural numbers multiple of 5 that are between 1 and 100 using the 'for' loop
and the 'while' loop.
4- Display the sum of 10 integer numbers as well as their average.
5- Display the sum of n integer numbers (n>0) as well as their average.
6- Read an integer x>0, then double it as many times until it exceeds 60.
7- Display the minimum between 10 integer numbers.
8- Display the minimum between n integer numbers (n>0).
9- Calculate the sum of several given integer numbers and stop as soon as the sum exceeds
500, then display their average.
10- Calculate the sum of several given integers, and stop when entering -1, display their
average.
Exercise 3 : (Course)
Write an algorithm that reads n real values between 0 and 20 (which represent the averages of n
students with 0<n≤100) and displays :
• The percentage of students with an average < 10
• The percentage of students with an average ≥ 10 and < 15
• The percentage of students with an average ≥ 15
The algorithm must take into account incorrect entries (values < 0 and > 20). In this case, the
user is asked to re-enter the value until it is correct.
Exercise 4 : (Course)
Write an algorithm that :
1) Ask for a number x between 10 and 20, until its value is correct. If answered
with a value > 20 the message: “Error: the value entered is large!” will be
displayed, and if answered with a value <10 the message “Error: the value
entered is small!” will be displayed.
2) Displays the previous 10 numbers of x.
Example: if x=14 then we will display: 13, 12, 11, …, 4.
3) Displays the last sum of multiples of x that does not exceed the value 100.
Example : if x=14 then: S=14+28+42=84.
The result is: 84
5|Page
Saad Dahlab University blida 1 1st Year / MI
Faculty of Sciences DW N°4 : Iterative Structures Algo1
Exercise 5 : (Course)
1) Write an algorithm which determines, from a list of real numbers all different from zero, the
number of elements greater than a given real value val. The stop will be made as soon as the
value 0 is read.
2) Write an algorithm that calculates the last sum that does not exceed 100 from a list of integers.
Exercise 6 : (Course)
1- Write an algorithm that reads two positive integers A, B and calculates their product using
only additions.
2- Write an algorithm that reads two positive integers A, B and calculates the quotient and
remainder of A by B without using division operators.
Exercise 7 : (Course)
Write the algorithms that calculate the values of the following expressions :
a)
b) {
c)
Exercise 8 : (DW)
Write an algorithm that reads an even integer x (with x>1) and then indicates whether x is
a purely even number or not.
An integer is said to be purely even if all its divisors except 1 are even.
Example: if x=32:
The divisors of x are: 2, 4, 8, 16, 32. So x=32 is a purely even integer.
Exercise 9 : (DW)
Consider the following algorithm :
Algorithm Exo;
Variables A, i, cmp : integer ;
Begin
Print ("Give the value of A :") ; Read (A) ;
cmp1 ; i1 ;
While (i <= A div 2) do
If (A mod i=0) then
cmpcmp+1 ;
EndIf ;
ii+1 ;
EndWhile ;
Print ("cmp= ", cmp) ;
End.
- Unroll this algorithm and give the display for A=6 then A=9.
- Indicate what this algorithm does.
6|Page
Saad Dahlab University blida 1 1st Year / MI
Faculty of Sciences DW N°4 : Iterative Structures Algo1
Exercise 10 : (DW)
Write an algorithm that reads an integer n (n>0) then displays:
1) All its divisors
2) Its first 15 multiples starting with 0.
3) Its multiples which are between 1 and 100.
Exercise 11 : (DW)
Write an algorithm that determines whether an integer M>0 is prime or not. A number is said to
be prime if it has exactly two different divisors.
Exercise 12 : (DW)
Based on the MOD and DIV operators, write an algorithm which:
1) Reads an integer x (x>0).
2) Calculates and displays the number of digits of x and constructs its inverse.
Example: if x=187 then: the number of digits of x is 3, and its inverse is: 781.
Reminder: 187 mod 10 = 7, and 187 div 10 = 18
Exercise 13 : (DW)
Write algorithms that determine the nth term of sequences defined by:
a) U1=1, U2=2, Un=Un-1+Un-2
b) S0=2, S1=3, S2=-2, Sn=Sn-3+ (-1)n *Sn-1 avec n ≥3.
7|Page