ProgrammingHardTCS NQT · Cognizant GenC Next
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer count = 0, s
for (each n from 2 to 500)
s = 1
for (each d from 2 to n - 1)
if (n mod d == 0)
s = s + d
end if
end for
if (s == n)
count = count + 1
end if
end for
Print count- A. 2
- B. 3
- C. 4
- D. 1
Correct answer: B. 3
s is the sum of the proper divisors of n (1 included), so the loop counts perfect numbers up to 500: 6, 28 and 496. Output 3.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division) Integer count = 0, n, s, d for…
- What will be the output of the following pseudocode? Integer c = 0 for (each y from 1890 to 2010) if ((y mod 4 == 0 AND y mod 100 != 0) OR…
- What will be the output of the following pseudocode? (AND has higher precedence than OR) Integer a = 5, b = 2, c = 3 if (a > b OR b > c…
- What will be the output of the following pseudocode? Integer calls = 0 Function f(Integer m, Integer n) calls = calls + 1 if (m == 0 OR n…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division) Integer a = 1, b = 100, steps…
- What will be the output of the following pseudocode? Integer a[5] = {7, 7, 5, 7, 6} Integer m1 = -1, m2 = -1 for (each i from 0 to 4) if…