answersLogoWhite

0

Swapping without third variable

Updated: 8/16/2019
User Avatar

Wiki User

14y ago

Best Answer

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;

}

}

User Avatar

Wiki User

14y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Swapping without third variable
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is program in c to swap entered two numbers without using third variable?

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); }


Swapping of number with using third variable?

To swap two numbers N1 and N2, using a third variable T... T = N1; N1 = N2; N2 = T;


Swap logic without using third variable?

a ^= b; b ^= a; a ^= b;


How do you swap in c programming?

the simple way can be explained by example as: let there be two integers as : int a=10,b=5; if we want to use third variable then let third variable be int c; and sorting is done as : c=a; a=b; b=c; if it is to be done by without using third variable then : a=a+b; b=a-b; a=a-b; at last the variable is sorted.


Write C coding for swamping the values of two variables without using a third variable?

I'll assume you meant to say: Swapping instead of Swamping. You would need to perform the XorSwap algorithm: void XorSwap(int *x, int *y) { if(x != y) { *x ^= *y; *y ^= *x; *x ^= *y; } } You can read more about this algorithm on Wikipedia.


Can you write addition of 2 no program without using third variable?

Yes. int a, b; a= 2; b= 3; a= a+b;


What is moderating variable?

Moderation occurs when the relationship between two variable depends on a third variable. The third variable is referred to as the moderate variable or simply the moderator


How do you write a program in C plus plus plus plus How do you write a program in C to swap two variables without using the third oneo swap two variables without using the third one?

To swap two variables without using a third variable, use exclusive or manipulation... a ^= b; b ^= a; a ^= b;


Are there any third party brokers who provide ing variable annuities?

Yes there are many third party brokers who are willing to provide ING variable annuities. Third party brokers are the most common at providing ING variable annuities.


Third variable problem?

the third variable problem is when an unintentional third variable influences two separate variables that are being measured. this causes a random and coincidental relationship between the two variables. An example would be as ice cream consumption goes up the number of drownings also goes up. the unintentional third variable in this case would be the heat.


What is an apparent although false association between two variable that is caused by some third variable?

Spurious Correlation.


WAP to interchange the value of two variable without third variable?

Ellipses (...) used to emulate indentation... swap (int *i1, int *i2) { /* only works for integers, i1 != i2 */ ... *i1 = *i1 ^ *i2; ... *i2 = *i1 ^ *i2; ... *i1 = *i1 ^ *i2; }