If the variables are x1 & x2
the solution is :
1) x1=x1+x2;
2) x2=x1-x2;
3) x1=x1-x2;
EX: x1=1 , x2=6;
1) x1= 1+6 = 7
2) x2= 7-6 =1
3 x1=7-1 =6
============================================
To swap two variables without using a third variable, use exclusive or manipulation... a ^= b; b ^= a; a ^= b;
Use list assignment i.e. for two variables $a, $b: ($a,$b) = ($b,$a)
a=a^b; b=a^b; a=a^b;
By using a third temporary variable. $tmp = $a; $a = $b; $b = $tmp;
Consider the following declarations:int x = 0;int y = 1;In order to swap the values, we need to use a temporary variable:int t = x;x = y;y = t;However, it is possible to swap the values without using a third variable:x ^= y ^= x ^= y;
You can swap two variables, by storing one of them temporarily in a third variable, like this: temp = a; a = b; b = temp; Inside a function, this won't work, because the function parameters are COPIES of the original variables, not the variables themselves. Any change won't affect the original variables. If you work with OBJECTS, and swap the CONTENTS of the objects (not the object pointers), it can work, though.
By using the algorithm of bitwise EORing (Exclusive ORing) the numbers together:If the two numbers are X and Y, then to swap them:X = X EOR YY = Y EOR XX =X EOR Ywill swap them.With knowledge of that algorithm, one then uses the syntax of Javascript to implement it.
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);
a ^= b; b ^= a; a ^= b;
The required c program is given below /*Swapping(interchange) the two entered numbers*/ #include<stdio.h> main() { /*Without using third variable*/ int a,b,t; printf("Enter a:"); scanf("%d",&a); printf("Enter b:"); scanf("%d",&b); a=a+b; b=a-b; a=a-b; printf("\n After swapping without using third variable"); printf("\na=%d\nb=%d",a,b); }
There are two ways in which you can swap without a third variable. 1. Using xor operation swap( int *a, int *b) { *a = *a ^ *b; *b = *a ^ *b; *a = *a ^ *b; } 2. Using addition and subtraction swap( int *a, int *b) { *a = *a + *b; *b = *a - *b; *a = *a - *b; } }
Is impossible without doing things you really shouldn't be doing. For example with strings you might try concatenating them together and splitting it back apart to accomplish a swap, or with integers you might be able to use math to do this. None of these techniques will generalize to an arbitrary data type, however, so my recommendation is: Just use a third variable and do a standard swap: Thing first = ... Thing second = ... Thing swap = first; first = second; second = swap; Is impossible without doing things you really shouldn't be doing. For example with strings you might try concatenating them together and splitting it back apart to accomplish a swap, or with integers you might be able to use math to do this. None of these techniques will generalize to an arbitrary data type, however, so my recommendation is: Just use a third variable and do a standard swap: Thing first = ... Thing second = ... Thing swap = first; first = second; second = swap;