22,310 views
44 44 votes

Consider the following program fragment for reversing the digits in a given integer to obtain a new integer.

Let $n = d_1\, d_2\, \ldots\, d_m$.

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. $n=d_1\, d_2 \,\ldots\, d_{m-i} \qquad \mathbf{and} \qquad \text{rev} = d_m\,d_{m-1} \,\ldots\, d_{m-i+1}$

  2. $n= d_{m-i+1} \,\ldots\, d_{m-1}\, d_m \qquad \mathbf{or} \qquad \text{rev} = d_{m-i} \,\ldots\, d_2\,d_1$

  3. $n \neq \text{rev}$

  4. $n=d_1\, d_2 \,\ldots\, d_m \qquad \mathbf{or} \qquad \text{rev} =d_m \,\ldots\, d_2\, d_1$

6 Answers

Best answer
51 51 votes

A loop invariant is something that hold at the start of a loop, across each iteration (inside an iteration it can change but before the iteration ends original condition must be true) and at the end also. So, we can check for the satisfiability of the condition at the loop header for start of the loop, for each iteration and also at the exit.

Here, in each iteration the right most digit of n, is moving to the right end of rev. So, answer is (A). i.e. the $2$ conditions given in $(A)$ choice are true on entry to loop, after each iteration (not necessarily during an iteration), and at end of loop.

• edited by
37 37 votes

Loop invariant is a condition which is true in every iteration.

So lets take an example say n = 123 where d1=1,d2=2,d3=3

  Iteration 1 :    rev = 3  n = 12    or rev = d3  and n = d1d2
  Iteration 2:    rev = 32  n = 1    or rev = d3d2  and n = d1
  Iteration 3:    rev = 321  n = 0    or rev=d3d2d1  and n=d0

So in general we are getting n = d1d2...dm-i and rev = dmdm-1.....dm-i+1 in every iteration. So option A is true.n=d1d2…dm−i and rev=dmdm−1…dm−i+1

6 6 votes

we can solve it by leting some values like n=123 and then check by option for ith iteration i.e. i=1 or i=2. Option A will satisfy.

4 4 votes

Loop invariant must hold at the end of the iteration. In the given code, the least significant digit is taken from n and added to rev. So, at the end of ith iteration, n will have its least significant bits removed and they will be seen in rev. So, answer is (A).

1 1 vote
Hello sir,

I have taken n=128,  after running code I got below output at each iteration:

Iteration 1(n=12, rev=8)

Iteration 2(n=1, rev=82)

Iteration 3(n=0, rev=821)

Now, as above output I can conclude that option c(n!=rev) should be right answer.

Please clarify if observed something wrong, thanks!
0 0 votes

The loop invariant here means a condition that remains true after every iteration of the loop. In this program, after each iteration, the last digit of n is removed and added to rev. So, n always stores the remaining left (unprocessed) digits, while rev stores the right-side digits that have already been processed in reverse order. For example, if n = 1234, after the first iteration n = 123 and rev = 4, after the second iteration n = 12 and rev = 43. This pattern remains true in every iteration, which is why option A is the correct loop invariant.

Answer:
Position:
Show:

Related questions

39 39 votes
5 answers 5 answers
11.9k
11.9k views
Kathleen asked Sep 18, 2014
11,895 views
Choose the best matching between the programming styles in Group 1 and their characteristics in Group 2.$$\begin{array}{|ll|ll|}\hline \rlap{\textbf{Group 1}} & & \rlap{...
80 80 votes
9 answers 9 answers
24.3k
24.3k views
Akash Kanase asked Feb 12, 2016
24,324 views
The following function computes $X^{Y}$ for positive integers $X$ and $Y$.int exp (int X, int Y) { int res =1, a = X, b = Y; while (b != 0) { if (b % 2 == 0) {a =...
75 75 votes
8 answers 8 answers
26.4k
26.4k views
Misbah Ghaya asked Feb 13, 2015
26,358 views
Consider the following pseudo code, where $x$ and $y$ are positive integers.begin q := 0 r := x while r ≥ y do begin r := r - y q := q + 1 end endThe post condition that ...
34 34 votes
4 answers 4 answers
9.0k
9.0k views
Kathleen asked Sep 12, 2014
9,011 views
Consider the following PASCAL program segment:if i mod 2 = 0 then while i >= 0 do begin i := i div 2; if i mod 2 < 0 then i := i - 1; else i := i – 2; end;An appropriate...