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:
- A.
n = D1D2…Dm−i and rev = DmDm−1…Dm−i+1
- B.
n = Dm−i+1…Dm−1Dm and rev = Dm−1…D2D1
- C.
n ≠ rev
- D.
n = D1D2…Dm and rev = DmDm−1…D2D1
Attempted by 336 students.
Sign up free to check your answer
Sign up freeLoading lesson…

