Professional Documents
Culture Documents
Total points: 23
Time: 35 minutes
2. Give 3 different values that could be used as input for X such that the following
algorithm prints 4. [3]
1 input X
2 X = X + 2
3 X = X div 3
4 output X
Solution. 10,11,12
3. Write an algorithm that will take as input a number less or equal to 9999 and
will print the number of its digits that are equal to 3. Your algorithm should
work as in the following examples. [7]
Input: 46
Output: 0
Input: 532
Output: 1
Input: 373
Output: 2
Solution.
1 input N
2 COUNT = 0
3 if N mod 10 = 3 then
4 COUNT = COUNT + 1
5 end if
6 if N div 10 mod 10 = 3 then
7 COUNT = COUNT + 1
8 end if
9 if N div 100 mod 10 = 3 then
10 COUNT = COUNT + 1
11 end if
12 if N div 1000= 3 then
13 COUNT = COUNT + 1
14 end if
15 output COUNT
4. What will the following algorithm print if we give 4 and 5 as input for X and
Y respectively? [2]
1 input X , Y
2 Z = 0
3 if ( X + Y ) mod 3 = 0 then
4 X = X + 2
5 if Y * Y < 30 then
6 Z = X * Y
7 end if
8 else
9 Y = Y + 2
10 if X * X > 80 then
11 Z = X div Y
12 end if
13 end if
14 output Z
Input: 127
Output: 2 x 50-euro, 1 x 20-euro, 1 x 5 -euro and 2 x 1-euro.
Input: 532
Output: 6 x 50-euro, 1 x 20-euro, 2 x 5 -euro and 2 x 1-euro.
Solution.
1 input SUM
2 FIFTY = SUM div 50
3 TWENTY = SUM mod 50 div 20
4 FIVE = SUM mod 50 mod 20 div 5
5 ONE = SUM mod 5
6 output FIFTY , " x 50 - euro , " , TWENTY , " x 20 - euro " ,
7 FIVE , " x 5 - euro and " , ONE , " x 1 - euro . "