ProgrammingHardTCS NQT · Cognizant GenC Next
What will be the output of the following pseudocode? (All variables are integers, so / is integer division)
What will be the output of the following pseudocode? (All variables are integers, so / is integer division)
Integer fact[10], count = 0, s, n
fact[0] = 1
for (each i from 1 to 9)
fact[i] = fact[i - 1] * i
end for
for (each x from 1 to 1000)
s = 0
n = x
while (n > 0)
s = s + fact[n mod 10]
n = n / 10
end while
if (s == x)
count = count + 1
end if
end for
Print count- A. 1
- B. 2
- C. 3
- D. 4
Correct answer: C. 3
The loop counts numbers equal to the sum of the factorials of their digits: 1 (1!), 2 (2!) and 145 (1! + 4! + 5! = 1 + 24 + 120). count = 3; the next one, 40585, is beyond 1000.
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 n = 50, c = 0, p = 2…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division) Integer n = 36, s = 0 for…
- What will be the output of the following pseudocode? (& is bitwise AND) Integer mask = 13, s, c = 0 s = mask while (s > 0) c = c + 1 s =…
- What will be the output of the following pseudocode? (integers are 32-bit; & is AND, << and >> are shifts) Integer x = 45 Integer y = (x…
- What will be the output of the following pseudocode? // BST: smaller keys go left, larger (or equal) keys go right Insert into an empty…
- What will be the output of the following pseudocode? Insert into an empty BST, in this order: 8, 3, 10, 1, 6, 14, 4, 7, 13 Queue q =…