How to swap 2 numbers without a 3rd variable

0 votes

How to swap 2 numbers without a 3rd variable?

I'm trying to understand how to swap two numbers without using a third variable. I’ve heard it’s possible through arithmetic or bitwise operations, but I’m unsure of the exact steps. What’s the best approach to swap values efficiently without needing extra storage?

Oct 28 in Web Development by Nidhi
• 2,660 points
41 views

1 answer to this question.

0 votes

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);
answered Nov 4 by kavya

Related Questions In Web Development

+1 vote
1 answer

How to access the Angularjs scope of a particular html element from our console?

Hello, You should follow the below steps:-- 1.Compile and ...READ MORE

answered Jan 21, 2020 in Web Development by Niroj
• 82,840 points

edited Jan 21, 2020 by Niroj 2,971 views
0 votes
1 answer

How to track with Google Analytics on a redirection page with PHP?

Hello @kartik, Since the page that is sending ...READ MORE

answered Jul 7, 2020 in Web Development by Niroj
• 82,840 points
1,833 views
0 votes
1 answer

How to deny direct access to a folder and file by htaccess?

Hello @kartik, Just move the includes folder out of the ...READ MORE

answered Aug 24, 2020 in Web Development by Niroj
• 82,840 points
9,475 views
0 votes
1 answer

How to download a file by jQuery.Ajax?

Hello @kartik, You don't need to do this ...READ MORE

answered Sep 18, 2020 in Web Development by Niroj
• 82,840 points
7,508 views
0 votes
1 answer

How to access url for the current if statement of laravel?

Hello @ subham , If you want to access the ...READ MORE

answered Aug 7, 2020 in Laravel by Niroj
• 82,840 points
1,374 views
0 votes
1 answer

How can i insert data in relation table using model?

Hello @Alisha, Try to work using the model ...READ MORE

answered Aug 24, 2020 in Java-Script by Niroj
• 82,840 points
1,339 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

How to update Angular version in a project?

Angular is a powerful framework for building ...READ MORE

answered Nov 4 in Web Development by kavya
74 views
0 votes
1 answer

How to create a horizontal line in CSS?

The most basic way to create a ...READ MORE

answered Nov 4 in Web Development by kavya
52 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP