Consider the following program fragment for reversing the di

Consider the following program fragment for reversing the digits in a given integer to obtain a new integer. Let n = D1D2…Dm

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 ith iteration is : (GATE CS 2004)

  1. n = D1D2….Dm-i and rev = DmDm-1…Dm-i+1
  2. n = Dm-i+1…Dm-1Dm and rev = Dm-1….D2D1
  3. n =! rev
  4. n = D1D2….Dm and rev = DmDm-1…D2D1
Answer

Answer: 1. n = D1D2….Dm-i and rev = DmDm-1…Dm-i+1

0Comment