answersLogoWhite

0

example:

x = x-y;

y = y-x;

x = y-x;

<><><>

It is not possible to swap two variables without using a temp variable. The code in the answer above, while clever, does not swap the variables. It will exchange the variable values for certain values of x and y, e.g. when x and y are small integers. If x and y are other values, such as strings, pointers, Infinity, NaN (not a number), floating point, or near the limits of the representation (causing over/underflow) then the code will not "swap" the values.

<--->

Note: there is absolutely no point in swapping two variables without using temopral variable, it's just a typical homework question, already asked here countless times.

Another variation:

a ^= b;

b ^= a;

a ^= b;

User Avatar

Wiki User

15y ago

What else can I help you with?

Related Questions

What is the advantage of swapping two variables without using 3 variable?

Nothing. Never do that.


Swapping of number with using third variable?

To swap two numbers N1 and N2, using a third variable T... T = N1; N1 = N2; N2 = T;


What is the difference between controlling variables and using controls?

Controlling variables is when you make sure that only one variable is being tested at a time and that there are not other variables that will make your results unclear. Using a control is when you do a trial without the variable to see what the normal results are.


How can you find a variable in math?

You find, or construct, an equation or set of equations which express the unknown variable in terms of other variables. Then you solve the equation(s), using algebra.You find, or construct, an equation or set of equations which express the unknown variable in terms of other variables. Then you solve the equation(s), using algebra.You find, or construct, an equation or set of equations which express the unknown variable in terms of other variables. Then you solve the equation(s), using algebra.You find, or construct, an equation or set of equations which express the unknown variable in terms of other variables. Then you solve the equation(s), using algebra.


How do you solve variables on both sides equation using integer rules?

You first have to get rid of the numbers that don't have variables. then you divide by the variable and solve for it.


How do you swap variables in PHP?

By using a third temporary variable. $tmp = $a; $a = $b; $b = $tmp;


How do you write a c program to swap the values of two variables in unix?

#include&lt;stdio.h&gt; void main() { int a=2,b=4; printf("Program for swapping two numbers "); printf("Numbers before swapping"); printf("a=%d and b=%d",a,b); a=((a+b)-(b=a)); printf("Numbers after swapping"); printf("a=%d and b=%d",a,b); getch(); }


Why is using two manipulated variables in an experiment would not work?

Using two manipulated variables in an experiment can make it difficult to determine which variable is actually causing a change in the outcome. This can lead to confounding variables and make it challenging to draw clear conclusions about the relationship between the variables and the outcome. It's important to isolate and study one variable at a time to accurately understand its impact.


What is independent variables?

an independent variable is a variable that changes the dependent variable.___________________________________________________Independentvariableis:a factor or phenomenon thatcausesorinfluencesanotherassociatedfactor or phenomenon called adependent variable. For example,incomeis an independentvariablebecause it causes and influences another variableconsumption. In a mathematicalequationormodel, the independent variable is the variable whosevalueis given. In anexperiment, it is the controlledcondition(that is allowed tochangein asystematicmanner) whose effect on thebehaviorof a dependent variable is studied. Also calledcontrolled variable,explanatory variable, orpredictor variable.


Swapping of 2 numbers using java language?

Use a temporary variable. Example (swap variables "a" and "b"): int a = 5; int b = 10; // Swap the variables int temp; temp = a; a = b; b = temp; System.out.println("a = " + a); System.out.println("b = " + b);Use a temporary variable. Example (swap variables "a" and "b"): int a = 5; int b = 10; // Swap the variables int temp; temp = a; a = b; b = temp; System.out.println("a = " + a); System.out.println("b = " + b);Use a temporary variable. Example (swap variables "a" and "b"): int a = 5; int b = 10; // Swap the variables int temp; temp = a; a = b; b = temp; System.out.println("a = " + a); System.out.println("b = " + b);Use a temporary variable. Example (swap variables "a" and "b"): int a = 5; int b = 10; // Swap the variables int temp; temp = a; a = b; b = temp; System.out.println("a = " + a); System.out.println("b = " + b);


Write a programme to exchange the content of 2 variables using another variable?

t=x; /* t is the other variable */x=y;y=t;


What are register variables What are the advantage of using register variables?

Asks the compiler to devote a processor register to this variable in order to speed the program's execution. The compiler may not comply and the variable looses it contents and identity when the function it which it is defined terminates.