ProgrammingModerateSSC CGL Tier 2 · IBPS PO Mains
A product of ten consecutive numbers 1 x 2 x ... x 10 is computed. The procedure below counts how many of the numbers 1 to 10 are divisible by 2 or by 3. What is printed?
A product of ten consecutive numbers 1 x 2 x ... x 10 is computed. The procedure below counts how many of the numbers 1 to 10 are divisible by 2 or by 3. What is printed?
count = 0
for i = 1 to 10
if i mod 2 == 0 or i mod 3 == 0 then count = count + 1
end for
Print count- A. 1
- B. 14
- C. 7
- D. 6
Correct answer: C. 7
The numbers 2, 3, 4, 6, 8, 9 and 10 are divisible by 2 or 3, giving 7. Requiring both rules gives 1 (only 6), doubling each count gives 14, and stopping at 9 drops 10 and gives 6.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- Smallest n such that n! has at least 5 trailing zeros is searched for by the procedure below, where z(n) is the number of factors 5 in n!…
- A number N = 1296 is reduced by the procedure below, which divides by the smallest prime factor each time it can and keeps a count of the…
- The procedure below counts the numbers from 1 to 1000 whose digit sum is exactly 5. It uses digit-sum by repeated division. What is…
- The procedure below finds how many integers x with 1 <= x <= 200 satisfy x^2 mod 13 == 1. What is printed? count = 0 for x = 1 to 200 if…
- The procedure below computes the value of the sum 1^2 + 2^2 + ... + 10^2 using the formula n(n+1)(2n+1)/6 for n = 10, and then the loop…
- Count the integers n with 1 <= n <= 1000 that are divisible by 7 but NOT divisible by 11. The procedure below does this with one loop…