Home » Programming & Data Structure » Programming and data structure miscellaneous » Question

Programming and data structure miscellaneous

Programming & Data Structure

  1. Consider the following program fragment for reversing the digits in a given integer to obtain a new integer. Let n = d1 d2... 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
    1. n = d1 d2 ...dm–i and rev = dm dm-1 ...dm-j + 1
    2. n = dm – i + 1 ...dm – 1 dm or rev = dm - i...d2 d1
    3. n ≠ rev
    4. n = d1 d2 ...dm or rev = dm ...d2 d1
Correct Option: A

NA



Your comments will be displayed only after manual approval.