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, 2024 in Web Development by Nidhi
• 6,040 points
78 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, 2024 by kavya

Related Questions In Web Development

0 votes
1 answer

How to create an Observable from a string in Angular 2?

In Angular (or any JavaScript application using ...READ MORE

answered Nov 27, 2024 in Web Development by kavya
76 views
+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 3,085 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,930 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,531 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,453 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,417 views
0 votes
1 answer

How can I implement pagination for large datasets in an Express.js API?

Pagination is a technique used to divide ...READ MORE

answered Oct 25, 2024 in Web Development by kavya
215 views
0 votes
1 answer

How do you implement API request validation in Express using middleware?

1. Create Middleware Function :  - Define a ...READ MORE

answered Oct 25, 2024 in Web Development by kavya
178 views
0 votes
1 answer

How to update Angular version in a project?

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

answered Nov 4, 2024 in Web Development by kavya
163 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, 2024 in Web Development by kavya
104 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