ProgrammingModerateSSC CGL Tier 2 · IBPS PO Mains
The procedure below counts the integers n with 1 <= n <= 60 that are not divisible by 2, 3 or 5 (coprime to 30). What is printed?
The procedure below counts the integers n with 1 <= n <= 60 that are not divisible by 2, 3 or 5 (coprime to 30). What is printed?
count = 0
for n = 1 to 60
if n mod 2 != 0 and n mod 3 != 0 and n mod 5 != 0 then count = count + 1
end for
Print count- A. 20
- B. 16
- C. 58
- D. 14
Correct answer: B. 16
The numbers from 1 to 60 coprime to 30 are 1, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 49, 53 and 59, which is 16. Ignoring the test for 5 gives 20, using or instead of and counts almost every number, and also testing for 7 removes 7 and 49.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- The procedure below counts the pairs i < j in the array [3, 1, 4, 1, 5] whose sum is even. What is printed? count = 0 for i = 0 to n - 2…
- The procedure below collects x mod 5 for every x from 1 to 20 into a set and prints the size of the set. What is printed? seen = empty set…
- Array a = [1, 1, 1, 1] (0-based). From index i you may jump forward by at most a[i] positions. The greedy below counts the minimum number…
- The procedure below sums the decimal digits of 2^10 = 1024 once and prints the result. What is printed? x = 2^10; total = 0 while x > 0…
- The procedure below counts the inversions of the array [2, 4, 1, 3] (pairs i < j with a[i] > a[j]) by checking all pairs. What is printed?…
- The procedure below counts the integers from 1 to 29 that are divisible by 3 or by 5. What is printed? count = 0 for n = 1 to 29 if n mod…