Consider the following program fragment for reversing the digits of a positive…

PYQ GATE 2004

Consider the following program fragment for reversing the digits of a positive integer to obtain a new integer. Let n = D1D2…Dm, where Dj denotes the j-th decimal digit from the left.

int n, rev;
rev = 0;
while (n > 0)
{
   rev = rev*10 + n%10;
   n = n/10;
}

The loop-invariant condition at the end of the i-th iteration is:

  1. A.

    n = D1D2…Dm−i and rev = DmDm−1…Dm−i+1

  2. B.

    n = Dm−i+1…Dm−1Dm and rev = Dm−1…D2D1

  3. C.

    n ≠ rev

  4. D.

    n = D1D2…Dm and rev = DmDm−1…D2D1

Attempted by 336 students.

Sign up free to check your answer

Sign up free

Explore the full course: Zero To Hero

Loading lesson…