how to swap two numbers with out using temp variable

Answers were Sorted based on User's Feedback



how to swap two numbers with out using temp variable..

Answer / sachin patil

a = 5;
b = 10;
a = a+b;
b = a-b;
a = a-b;

Is This Answer Correct ?    55 Yes 5 No

how to swap two numbers with out using temp variable..

Answer / vishal gupta

well, use math:

x=5;
y=7;
x = x * y;
y = x / y;
x = x / y;

And the numbers have been swapped without the use of an
extra variable. I hope it helped.

Is This Answer Correct ?    44 Yes 8 No

how to swap two numbers with out using temp variable..

Answer / guest

another way is using bitwise XOR (^).

a=9; //a=1001
b=5; //b=0101

a=a^b; //a=1100
b=a^b; //b=1001
a=b^a; //a=0101

Is This Answer Correct ?    29 Yes 6 No

how to swap two numbers with out using temp variable..

Answer / a.g.dhivyalakshmi

//swaping of 2 numbers without using temp variable
//a=5 & b=6
a=a+b; //a=30
b=a-b; //b=5
a=a-b; //a=6

//Thus two numbers are swapped

// or
//a=5 & b=6
a=a*b; //a=30
b=a/b; //b=5
a=a/b; //a=6

//Thus two numbers are swapped

Is This Answer Correct ?    8 Yes 0 No

how to swap two numbers with out using temp variable..

Answer / raja

if a=10& b=20

a=a+b //a=30
b=a-b //b=10
a=a-b //a=20

finally we got a=20,b=10

Is This Answer Correct ?    10 Yes 3 No

how to swap two numbers with out using temp variable..

Answer / prasannaroy

a=10;
b=20;
b=a+b-(a=b);

Is This Answer Correct ?    8 Yes 2 No

how to swap two numbers with out using temp variable..

Answer / vaibhav meena

Its simple:
For Integer Values

A^=B^=A

and with arithmetic operators :

All the above answers are right

Is This Answer Correct ?    7 Yes 3 No

how to swap two numbers with out using temp variable..

Answer / pranav

//swaping of 2 numbers without using temp variable
//a=5 & b=6
a=a+b; //a=30
b=a-b; //b=5
a=a-b; //a=6

Is This Answer Correct ?    0 Yes 0 No

how to swap two numbers with out using temp variable..

Answer / arundhati

a^=b^=a^=b;

Is This Answer Correct ?    0 Yes 0 No

how to swap two numbers with out using temp variable..

Answer / lucky

All the above ans. are nice.
I proved my "INDIA" in which these comuter logicmind are
living.........

Is This Answer Correct ?    2 Yes 5 No

Post New Answer

More C++ General Interview Questions

List the features of oops in c++?

0 Answers  


What is set in c++?

0 Answers  


What is linked list in c++?

0 Answers  


What is data types c++?

0 Answers  


What is iomanip c++?

0 Answers  






You run a shell on unix system. How would you tell which shell are you running?

0 Answers  


How can you specify a class in C++?

0 Answers  


pls help.. paper bills.. 1000, 500, 100, 50, 20, 10, 5, 1.. create a program that will count all the paper bills in the number being input.. example: enter a number: 3886 there is/are: 3 ->1000 1 ->500 3 ->100 1 ->50 1 ->20 1 ->10 1 ->5 1 ->1 example2: enter a number: 728 there is/are: 0 ->1000 1 ->500 2 ->100 0 ->50 1 ->20 0 ->10 1 ->5 3 ->1

4 Answers  


What is a breakpoint?

0 Answers  


Does c++ have string data type?

0 Answers  


Do you know the use of vtable?

0 Answers  


describe private access specifiers?

0 Answers  


Categories