Swap Two Numbers Without Using a Third Variable

Your task is to swap two given numbers without utilizing an additional variable and print the swapped values.

Input:

The first line contains an integer 't', indicating the number of test cases.
The following lines each contain two integers 'a' and 'b'.

Output:

For each test case, output the swapped values of 'a' and 'b' as a single line.

Example:

Input:
2
3 7
10 5
Output:
7 3
5 10

Constraints:

  • 1 <= 't' <= 100
  • -105 <= 'a', 'b' <= 105
Note:
Focus only on implementing the given function, as output handling is already managed.
AnswerBot
4mo

Swapping two numbers without using a third variable in Java.

  • Use bitwise XOR operation to swap the numbers without using a third variable.

  • a = a XOR b; b = a XOR b; a = a XOR b; will swap the values of ...read more

Help your peers!
Select
Add answer anonymously...

Popular interview questions of Java Developer

A Java Developer was asked Q1. How do you swap two numbers without using a third variable?
A Java Developer was asked Q2. 1) @Controller & @RestController difference
A Java Developer was asked Q3. What is the Singleton Design Pattern?
Wunderman Thompson Commerce Java Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits