ProgrammingHardLTIMindtree · Cognizant GenC

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Function g(Integer n)
    if (n == 0)
        return 0
    end if
    if (n mod 2 == 0)
        return g(n - 1) + n
    else
        return g(n - 1) - n
    end if
end function
Print g(9)
  1. A. -9
  2. B. -5
  3. C. -4
  4. D. 5

Correct answer: B. -5

g(9) = -9 + 8 - 7 + 6 - 5 + 4 - 3 + 2 - 1 = -5.

More Pseudocode and Code Tracing questions

Exams that ask this topic