ProgrammingModerateTCS NQT · Infosys

Which expression computes ceil(a / b) for positive int a = 7 and b = 3 using integer arithmetic, written as ((a + b - 1) / b)?

  1. A. 2
  2. B. 3
  3. C. 4
  4. D. 7

Correct answer: B. 3

(7 + 3 - 1) / 3 = 9 / 3 = 3. The -1 before the division is what rounds up, and the expression is only safe when a + b - 1 cannot overflow.

More C Programming questions

Exams that ask this topic