Algorithms | Analysis of Algorithms | Question 19
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; } |
chevron_right
filter_none
The loop invariant condition at the end of the ith iteration is: (GATE CS 2004)
(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
Answer: (A)
Explanation: We can get it by taking an example like n = 54321. After 2 iterations, rev would be 12 and n would be 543.
Quiz of this Question
Recommended Posts:
- Algorithms | Analysis of Algorithms (Recurrences) | Question 4
- Algorithms | Analysis of Algorithms (Recurrences) | Question 2
- Algorithms | Analysis of Algorithms (Recurrences) | Question 7
- Algorithms | Analysis of Algorithms (Recurrences) | Question 9
- Algorithms | Analysis of Algorithms (Recurrences) | Question 8
- Algorithms | Analysis of Algorithms (Recurrences) | Question 3
- Algorithms | Analysis of Algorithms (Recurrences) | Question 11
- Algorithms | Analysis of Algorithms (Recurrences) | Question 11
- Algorithms | Analysis of Algorithms (Recurrences) | Question 11
- Algorithms | Analysis of Algorithms (Recurrences) | Question 6
- Algorithms | Analysis of Algorithms (Recurrences) | Question 1
- Algorithms | Analysis of Algorithms | Question 12
- Algorithms | Analysis of Algorithms | Question 18
- Algorithms | Analysis of Algorithms | Question 14
- Algorithms | Analysis of Algorithms | Question 5



