ProgrammingHardTCS NQT · Accenture pattern
What will be the output of the following pseudocode (Armstrong number check)?
What will be the output of the following pseudocode (Armstrong number check)?
Function isArmstrong(Integer n)
String s = toString(n)
Integer p = length(s)
Integer total = 0
for (each d in s)
total = total + (digitValue(d) ^ p)
end for
return total == n
End Function
Print isArmstrong(153), isArmstrong(123)- A. true true
- B. true false
- C. false true
- D. false false
Correct answer: B. true false
153 has 3 digits, and 1^3+5^3+3^3 = 1+125+27 = 153, matching n: true. 123 has 3 digits too, but 1^3+2^3+3^3 = 1+8+27 = 36, which does not equal 123: false.
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? Integer c = 0 for (each i from 1 to 3) for (each j from 1 to 3) if (j == 2) break end…
- What will be the output of the following pseudocode (Kadane's maximum subarray sum)? (Arrays are indexed from 0.) Integer arr[9] = {-2, 1…
- What is the time complexity of binary search on a sorted array of n elements? Function binarySearch(Array arr, Integer target) Integer lo…
- What is the worst-case time complexity of the following pseudocode (bubble sort) on an array of n elements? for (each i from 0 to n - 2)…
- What is the time complexity of the following recursive pseudocode (computing n choose k via Pascal's identity), in the worst case, as a…
- What is the space complexity (auxiliary stack space) of the following recursive factorial function for input n? Function fact(Integer n)…