#include<stdio.h>
void main()
{
int a,b;
printf("enter number 1\n");
scanf("%d",&a);
printf("enter number 2\n");
scanf("%d",&b);
printf("the swapped result is\n");
a=a^b;
b=a^b;
a=a^b;
printf("%d\n",a);
printf("%d\n",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;
#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(); }
Swapping means to swap the values of two addresses in main memory.
It means that you swap the values of that variables EX: -==- before swapping :- Variable1 = 5; variable2 = 10; after swapping :- Variable1 = 10; variable2 = 5;
swapping is nothing but interchanging the values of a given character for example : if a=5 , b=4 before swapping then it becomes a=4,b=5 after swapping
Swapping was an older form of memory management. It was moving from/to secondary storage a whole program at a time, in a scheme known as roll-in/roll-out. Now swapping is a fairly close synonym of paging.
To swap two integers in a program, you can use a temporary variable. Here's a simple example in Python: a = 5 b = 10 temp = a a = b b = temp Alternatively, you can swap them without a temporary variable using tuple unpacking: a, b = b, a Both methods will effectively swap the values of a and b.
// Swapping values of a and b int a = 1; int b = 50; int temp = a; // temp = 1 a = b; // a = 50 b = temp; // b = 1
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;
#include<conio.h> main() { int a,b; scanf("%d%d",&a,&b); printf("Before swapping A= %d B=%d",a,b); swap(a,b); getch(); } void swap(int a,int b) { int c; c=a; a=b; b=c; printf("After swapping A= %d B=%d",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
Assuming the numbers are in unsorted order, assign the first number to a temporary variable then traverse the remaining numbers, comparing each of their values to the temporary. If any value is smaller than the temporary, assign its value to the temporary. Once all values have been traversed and compared, the temporary variable holds the lowest value.