How to swap two numbers without using temp or third variable

本文介绍三种不用额外变量实现两个整数交换的方法:算术运算、位运算及乘除法。通过具体示例代码展示每种方法的实现过程,并讨论了它们的优缺点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

How to swap two numbers without using temp or third variable is common interview question not just on Java interviews but also on C and C++ interviews. It is also a good programming questions for freshers. This question was asked to me long back and didn't had any idea about how to approach this question without using temp or third variable, may be lack of knowledge on bitwise operators in Java or may be it didn't click at that time. Given some time and trial error, I eventually come out with a solution with just arithmetic operator but interviewer was keep asking about other approaches of swapping two variables without using temp or third variable. Personally, I liked this question and included in list of my programming interview question because of its simplicity and some logical work, it force you to do. When learned bit-wise operation in Java I eventually find another way of swapping two variables without third variable, which I am going to share with you guys.


Swapping two numbers without using temp variable in Java

swap two numbers without using thrid or temp variable in Java programmingIf you have ever heard this question, then you must be familiar with this approach of swapping numbers without using temp variable. If you are hearing it first time, then try it yourself, its a good programming exercise for absolute first timer. By the way, here is the code example of swapping two numbers without using temp variable and using arithmetic operator in Java:

int a = 10 ;
int b = 20 ;

System . out . println ( " value of a and b before swapping, a: " + a + " b: " + b );

//swapping value of two numbers without using temp variable
a = a + b ; //now a is 30 and b is 20
b = a - b ; //now a is 30 but b is 10 (original value of a)
a = a - b ; //now a is 20 and b is 10, numbers are swapped

System . out . println ( " value of a and b after swapping, a: " + a + " b: " + b );

Output :
value of a and b before swapping, a : 10 b : 20
value of a and b after swapping, a : 20 b : 10

Swapping two numbers without using temp variable in Java with bitwise operator

Bitwise operators can also be used to swap two numbers without using third variable. XOR bitwise operator returns zero if both operand is same i.e. either 0 or 1 and returns 1 if both operands are different e.g. one operand is zero and other is one. By leveraging this property, we can swap two numbers in Java. Here is code example of swapping two numbers without using temp variable in Java using XOR bitwise operand:

A B A ^ B ( A XOR B )
0 0 0 (zero because operands are same)
0 1 1
1 0 1 (one because operands are different)
1 1 0

int a = 2 ; //0010 in binary
int b = 4 ; //0100 in binary
System . out . println ( " value of a and b before swapping, a: " + a + " b: " + b );
//swapping value of two numbers without using temp variable and XOR bitwise operator
a = a ^ b ; //now a is 6 and b is 4
b = a ^ b ; //now a is 6 but b is 2 (original value of a)
a = a ^ b ; //now a is 4 and b is 2, numbers are swapped
System . out . println ( " value of a and b after swapping using XOR bitwise operation, a: " + a + " b: " + b );

value of a and b before swapping, a : 2 b : 4
value of a and b after swapping using XOR bitwise operation, a : 4 b : 2

Swapping two numbers without using temp variable in Java with division and multiplication

There is another, third way of swapping two numbers without using third variable, which involves multiplication and division operator. This is similar to first approach, where we have used + and - operator for swapping values of two numbers. Here is the code example to swap tow number without using third variable with division and multiplication operators in Java :

int a = 6 ;
int b = 3 ;

System . out . println ( " value of a and b before swapping, a: " + a + " b: " + b );

//swapping value of two numbers without using temp variable using multiplication and division
a = a * b ; //now a is 18 and b is 3
b = a / b ; //now a is 18 but b is 6 (original value of a)
a = a / b ; //now a is 3 and b is 6, numbers are swapped

System . out . println ( " value of a and b after swapping using multiplication and division, a: " + a + " b: " + b );

Output :
value of a and b before swapping, a : 6 b : 3
value of a and b after swapping using multiplication and division, a : 3 b : 6

That's all on 3 ways to swap two variables without using third variable in Java. Its good to know multiple ways of swapping two variables without using temp or third variable to handle any follow-up question. Swapping numbers using bitwise operator is the fastest among three, because it involves bitwise operation. It’s also great way to show your knowledge of bitwise operator in Java and impress interviewer, which then may ask some question on bitwise operation. A nice trick to drive interview on your expert area.



Read more: http://javarevisited.blogspot.com/2013/02/swap-two-numbers-without-third-temp-variable-java-program-example-tutorial.html#ixzz2khV8fuDT

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值