How to swap values between two variables without using a
third variable?

Answers were Sorted based on User's Feedback



How to swap values between two variables without using a third variable?..

Answer / srinivas

int a=2,b=3;
a=a+b-(b=a); /*now a=3, b=2*/

Is This Answer Correct ?    2 Yes 0 No

How to swap values between two variables without using a third variable?..

Answer / chitra

main()
{
int a,b;

a=a+b;
b=a-b;
a=a-b;
}

chitra write answer

Is This Answer Correct ?    1 Yes 0 No

How to swap values between two variables without using a third variable?..

Answer / kumar

//language using C#

int a=10;
int b=20;

a=a+b;
b=a-b;
a=a-b;

thanks
Kumar
http://www.sourcecodehub.com/php/how-to-swap-values-of-two-variables-without-using-a-third-variable-integer/

Is This Answer Correct ?    1 Yes 0 No

How to swap values between two variables without using a third variable?..

Answer / joel pinto

a^=b^=a^=b;

Is This Answer Correct ?    2 Yes 2 No

How to swap values between two variables without using a third variable?..

Answer / nisay mizrahi

double a,b;
System.out.println("put in 2 numbers");
a=in.nextDouble();
b=in.nextDouble();
if(a>b){
a=a-b;
b=b+a;
a=b-a;

}
else
if(b>a){
b=b-a;
a=a+b;
b=a-b;
}
System.out.println("a is "+a);
System.out.println("b is "+b);
}

}

Is This Answer Correct ?    2 Yes 2 No

How to swap values between two variables without using a third variable?..

Answer / neha

class SwapBitwise
{
public void SwapByBitwise(int x, int y)
{
x =x ^ y;
y = x ^ y;
x = x ^ y;
System.out.println("x : " + x + " " + "y :" + y);
}
}
public class SwapOperation
{
public static void main(String[] args) {
SwapBitwise obj = new SwapBitwise();
obj.SwapByBitwise(200, 300);
}

}

Is This Answer Correct ?    1 Yes 1 No

How to swap values between two variables without using a third variable?..

Answer / adil

string a = "adil ahmad", b = "anuj gupta";
a = a + b;
b = a.Substring(0, a.Length - b.Length);
a = a.Substring(b.Length, a.Length - b.Length)

Is This Answer Correct ?    0 Yes 0 No

How to swap values between two variables without using a third variable?..

Answer / padma

Need more information related to,
1. the values and type of variables before doing a swap, in
order to do the efficient memory management and code
robustness.
2.the reason to swap without using third variable.

3.code efficiency increased due to the two variable alone.

4.pre and post conditions for swap

5.the programming laguage to be used to do swap.

Is This Answer Correct ?    0 Yes 0 No

How to swap values between two variables without using a third variable?..

Answer / guest

using swap instruction in micro controller.

Is This Answer Correct ?    0 Yes 0 No

How to swap values between two variables without using a third variable?..

Answer / guest

using swap instruction in micro controller.

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More Programming Languages AllOther Interview Questions

When you deliver your C++ headers and C++ library of a class (what all can you change in the class so that application using your class does not need to recompile the code)

0 Answers   Yahoo,


Why multiple inheritance is removed in java?

4 Answers   TCS,


What is Console Application? & The purpose Of Console Application? with description.

1 Answers  


Write a program to reverse a number?

0 Answers   BirlaSoft,


Outline the two important features of a terminating recursion. Any ideas?

0 Answers  






19. Given a system that is described with the following equation, X=A+(B.(A̅+C)+C)+A.B.(D̅+E̅) a) Simplify the equation using Boolean Algebra. b) Implement the original and then the simplified equation with a digital circuit. c) Implement the original and then the simplified equation in ladder logic.

0 Answers   Bajak Paint,


what is the basic and unique feature of dotnet

0 Answers   HCL,


what is delegate and how works delegate and event.

0 Answers   NIIT,


What is good attributes of Programming language ?

6 Answers   GKN Land Systems,


what is difference between VB5 AND VB6.0

2 Answers   Satyam,


Given a Binary Search Tree, write a program to print the kth smallest element without using any static/global variable. You can?t pass the value k to any function also.

0 Answers   Amazon,


could u please also write an example of a code that involves instances from an abstract class just as u did for interfaces because u said it could also work which i really doubt. thanks

0 Answers  


Categories