ProgrammingHardInfosys SP · TCS Digital
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer a = 144, b = 89, t, steps = 0
while (b != 0)
t = a mod b
a = b
b = t
steps = steps + 1
end while
Print steps, a- A. 11 1
- B. 9 1
- C. 10 89
- D. 10 1
Correct answer: D. 10 1
144 and 89 are consecutive Fibonacci numbers, the worst case for Euclid: the remainders walk down 55, 34, 21, 13, 8, 5, 3, 2, 1, 0, which is 10 steps, ending with gcd 1. Output 10 1.
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; , / and mod have equal…
- What will be the output of the following pseudocode? (& is bitwise AND, >> is right shift) Integer s = 0, x, t for (each i from 1 to 16) x…
- What will be the output of the following pseudocode? Integer m[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} for (each i from 0 to 2) for…
- What will be the output of the following pseudocode? Integer n = 0, c = 0 do c = c + 1 n = n + 3 while (n mod 5 != 0) Print c, n
- What will be the output of the following pseudocode? Integer cat[0..6] cat[0] = 1 for (each i from 1 to 6) cat[i] = 0 for (each j from 0…
- What will be the output of the following pseudocode? Integer a[6] = {3, 1, 2, 6, 4, 5}, swaps = 0, m, n = 6 for (each i from 0 to n - 2) m…