#include<stdio.h>
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();
}
echo Enter valuefor a:
read a
echo Enter valuefor b:
read b
clear
echo Values of variables before swaping
echo A=$a
echo B=$b
echo Values of variables after swaping
a=`expr $a + $b`
b=`expr $a - $b`
a=`expr $a - $b`
echo A=$a
echo B=$b
The operating system is immaterial. There are two ways to swap two variables.
Using a temporary variable:
void swap (int* a, int* b) {
int temp;
temp = (*a);
(*a) = (*b);
(*b) = temp;
}
Without using a temporary variable:
void swap (int* a, int* b) {
(*a) ^= (*b) ^= (*a) ^= (*b);
}
Use list assignment i.e. for two variables $a, $b: ($a,$b) = ($b,$a)
void swap (int* a, int* b) { if (!a !b) return; // can't swap a pointer to null *a^=*b^=*a^=*b; }
You'll need 3 variables for this, here's a pseudo code for swapping values of 2 variables.ALGORITHM SWAPvar1 = 10var2 = 20temp = 0temp = var1 "temp = 10"var1 = var2 "var1 = 20, originally 10"var2 = temp "var2 = 10, originally 20"END SWAP
int a,b; a=a+b; b=a-b; a=a-b; that's it simple
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;
To swap two variables without using a third variable, use exclusive or manipulation... a ^= b; b ^= a; a ^= b;
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;
Use list assignment i.e. for two variables $a, $b: ($a,$b) = ($b,$a)
a=a^b; b=a^b; a=a^b;
void swap (int* a, int* b) { if (!a !b) return; // can't swap a pointer to null *a^=*b^=*a^=*b; }
You'll need 3 variables for this, here's a pseudo code for swapping values of 2 variables.ALGORITHM SWAPvar1 = 10var2 = 20temp = 0temp = var1 "temp = 10"var1 = var2 "var1 = 20, originally 10"var2 = temp "var2 = 10, originally 20"END SWAP
#include using namespace std; void swap(int &a, int &b); int main() { int x=5,y=3; cout
int a,b; a=a+b; b=a-b; a=a-b; that's it simple
It means that you swap the values of that variables EX: -==- before swapping :- Variable1 = 5; variable2 = 10; after swapping :- Variable1 = 10; variable2 = 5;
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;
int varA, varB; int tmp; // common way to swap 2 variables in the same type; tmp = varA; // store the original value of varA varA = varB; // assign A with B varB = tmp; // B now has the original value of varA