12) Given here is an algorithm to swap two numbers. Which of the following line will
replace "?" to complete the algorithm?
A) X=X+Y
B) X=XXY
C) X=X/Y
D) X=X-Y
Step 4: X = X - YStep 2: ?
Step 3: Y = XYStep 1: Read two numbers X and Y.
Answers
Answer:
A ID: 1102102807
X=X+Y
B
ID: 1102102807
X=X * Y
ID: 1102102807
X=Y - X
ID: 1102102807
X= X-Y
Answer:
Correct option is (A) X=X+Y. In the question step 3 must be Y= X - Y.
Explanation:
Let us understand the algorithm step by step:
Step 1 :
Read the values of X and Y.
Let us assume X = 10 and Y = 20
Step 2:
Let us use the equation X = X + Y
Now, X = 10 and Y = 20
Therefore, X = 10 + 20 = 30
So now new value of X is 30
Step 3:
Using equation Y = X - Y
Now values of X and Y are 30 and 20 respectively.
Y = 30 -20 = 10
So, now the new value of Y is 10.
Step 4:
Using equation X = X - Y
Now the values of X and Y are 30 and 10 respectively.
X = 30 - 10 = 20
So, now the new value of X is 20.
Conclusion:
Using the algorithm the value of X and Y are interchanged.
Now the value of X is 20 and Y is 10