You are on page 1of 3

Algorithms Quiz

Total points: 23
Time: 35 minutes

1. Determine the values of the following expressions [4]

(a) (157899 mod 100) + 3


(b) (88888 div 888888) + 10
(c) 455 mod 567 - 10
(d) 123456 div 1000 - 100

Solution. (a) 102


(b) 10
(c) 445
(d) 23

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
 

5. George works as an acountant in a bank. Each time clients ask to withdraw


an amount of money he needs to use as few banknotes and coins as possible.
Unfortunately until now he was doing all calculations by hand. Help George
by writing an algorithm that takes as input an amount in euros and converts
this amount to the smallest possible number of 50-euro bills, 20-euro bills, 5-
euro bills and 1-euro coins. Your algorithm should work as in the following
examples. [7]

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 . "
 

You might also like