void main() { int a,b; clrscr(); printf("\n\n\t\tenter any two nos..."); scanf("%d%d",&a,&b); a=a+b; b=a-b; a=a-b; printf("\n\n\t\tvalue of a=",a); printf("\n\n\t\tvalue of b=",b); getch(); }
Use the bitwise XOR assignment operator (^=) three times, alternating the operands each time.
Note that the operands must be distinct -- they may be the same value, but they must be different variables (in completely different memory locations). XORing a variable with itself yields the value zero, so if x=1, x^=x would make x=0, thus the "swap" fails.
The following code demonstrates the algorithm.
#include <iostream>
int main()
{
int x = 42;
int y = 67;
printf( "Before:\tx=%d, y=%d\n", x, y );
x ^= y;
y ^= x;
x ^= y;
printf( "After:\tx=%d, y=%d\n", x, y );
return( 0 );
}
a=b+a;
b=a-b;
a=a-b;
with 3rd variable
If a=10
b=20
c=a; (now c=10 and a is empty)
a=b; ( b's value assign to a=20)
b=c; (now c assign value to b=10)
now it is swapped
a=20
b=10
Let us take a=40,b=50.Now after swapping,we should get the output as a=50,b=40.
main()
{
int a=40,b=50;
a=a+b;
b=a-b;
a=a-b;
printf("a=%d,b=%d",a,b);
}
In C++, the coding is :
#include <iostream.h>
#include <conio.h>
void main()
{
int a,b;
cout<<"type the value of a and b ";
cin>>a>>b;
a=a+b;
b=a-b;
a=a-b;
cout<<"after swapping the values are :";
cout<<a<<"\t"<<b<<"\n";
getch();
}
1)
num1 = num1 + num2;
num2 = num1 - num2;
num1 = num1 - num2;
Enter Number 1: 20
Enter Number 2:
10
After swapping, num1=10 and num2=20
2)
x -= y;
y += x; // y becomes x
x = (y - x); // x becomes y
3)
x = x*y;
y = x / y;
x = x / y;
A = A xor B B = A xor B A = A xor B in C... A^=B; B^=A; A^=B;
a ^= b; b ^= a; a ^= b;
There are three primary algorithms to exchange the values of two variables. Exchange with Temporary Variable temp = a; a = b; b = temp; Exchange Without Temporary Variable Using Exclusive Or a = a ^ b; b = b ^ a; a = a ^ b; Exchange Without Temporary Variable Using Arithmetic a = a + b; b = b - a; a = a - b;
Use list assignment i.e. for two variables $a, $b: ($a,$b) = ($b,$a)
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); }
To swap two variables without using a third variable, use exclusive or manipulation... a ^= b; b ^= a; a ^= b;
By using a third temporary variable. $tmp = $a; $a = $b; $b = $tmp;
Nothing. Never do that.
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.
A = A xor B B = A xor B A = A xor B in C... A^=B; B^=A; A^=B;
a ^= b; b ^= a; a ^= b;
There are three primary algorithms to exchange the values of two variables. Exchange with Temporary Variable temp = a; a = b; b = temp; Exchange Without Temporary Variable Using Exclusive Or a = a ^ b; b = b ^ a; a = a ^ b; Exchange Without Temporary Variable Using Arithmetic a = a + b; b = b - a; a = a - b;
a=a^b; b=a^b; a=a^b;
Use list assignment i.e. for two variables $a, $b: ($a,$b) = ($b,$a)
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); }
In java we access static variables and static methods without creating objects. i.e.,we can access directly by using classname we can also access static variables and static methods by using objects which are created by using class where the static variables and static methods are available
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; } }