xxxxxxxxxx
x = 10
y = 11
x, y = y, x
"Swapping by simultaneously creating a tuple and unpacking it"
print(x, y)
xxxxxxxxxx
// Another approach, that doesn't requires to create a third variable,
// but requires more computation
int a = 7;
int b = 2;
a = a ^ b;
b = a ^ b;
a = a ^ b;
// ^ means XOR
// if you want a more concise code, you can do
// a ^= b;
// b ^= a;
// a ^= b;
xxxxxxxxxx
//The code for a function to Swap two number with a temporary variable is as follows
#include<stdio.h>
void swapping(int, int); //function declaration
int main()
{
int a, b;
printf("Enter values for a and b respectively: \n");
scanf("%d %d",&a,&b);
printf("The values of a and b BEFORE swapping are a = %d & b = %d \n", a, b);
swapping(a, b); //function call
return 0;
}
void swapping(int x, int y) //function definition
{
int third;
third = x;
x = y;
y = third;
printf("The values of a and b AFTER swapping are a = %d & b = %d \n", x, y);
}
xxxxxxxxxx
A = A operation B
B = A inverse-operation B
A = A inverse-operation B