ProgrammingVery hardInfosys SP · Capgemini

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Integer total = 0
Procedure p(Integer n)
    if (n <= 0)
        return
    end if
    total = total + n
    p(n - 2)
    p(n - 3)
End Procedure
p(7)
Print total
  1. A. 21
  2. B. 25
  3. C. 28
  4. D. 41

Correct answer: B. 25

Let T(n) be the total added by p(n): T(n) = n + T(n − 2) + T(n − 3), T(n ≤ 0) = 0. Going up: T(1) to T(7) = 1, 2, 4, 7, 11, 17, 25, e.g. T(7) = 7 + T(5) + T(4) = 7 + 11 + 7 = 25.

More Pseudocode and Code Tracing questions

Exams that ask this topic