Suppose we have two numbers, 5 and 3.
Let:
a = 5 (First number)
b = 3 (Second number)
Swapping Logic:
a = a + b = 5 + 3 = 8
b = a - b = 8 - 3 = 5
a = a - b = 8 - 5 = 3
After swapping, the values are:
a = 3
b = 5
Algorithm:
STEP 1: START
STEP 2: ENTER a, b
STEP 3: PRINT initial values of a, b
STEP 4: a = a + b
STEP 5: b = a - b
STEP 6: a = a - b
STEP 7: PRINT swapped values of a, b
STEP 8: END
javascript code:
let a = 5; // First number
let b = 3; // Second number
console.log("Before swapping:");
console.log("a =", a);
console.log("b =", b);
// Swapping Logic
a = a + b; // Step 4: a becomes 8 (5 + 3)
b = a - b; // Step 5: b becomes 5 (8 - 3)
a = a - b; // Step 6: a becomes 3 (8 - 5)
console.log("After swapping:");
console.log("a =", a);
console.log("b =", b);