answersLogoWhite

0


Best Answer

you can do it in the following manner

Supposing your two variables are x and y:

int x=3;

int y=5;

x=x+y; [x becomes 8]

y=x-y; [y assumes the original value of x i.e. 3]

x=x-y; [x assumes the original value of y i.e. 5]

or... you can use the unary XOR (exclusive or) operator, '^=' .

Same values, int x=3, y=5

x ^= y; // x becomes 6

y ^= x; // y becomes 5

x ^= y; // x becomes 3.

The second method has more advantages :

- Its assembler operations never use the processor's ALU carry.

- Without use of the aforementioned carry, no overflow will ever occur.

- Performing this with 32-bits values 8-bits processors will be more efficient, in terms of program space AND execution speed.

- You can even use this in Visual Basic an other languages which implements boundaries on values, while the first method is guaranteed to fail when overflows occurs.

In both case, do not EVER try to factorize these 3 lines into two, as the operations order in multiple-operators lines depends on the compiler's way to parse your code.

Thus , typing

x = x ^ y ^x;

y = y ^ x;

Will surely give you garbage.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

#include<stdio.h>

#include<conio.h>

int main (void)

{

int a = 5;

int b = 8;

printf ( " Before swapping a = %d , b = %d\n",a,b);

swap(&a, &b);

printf ( " After calling swap function a = %d , b = %d\n",a,b);

getch();

return 0;

}

void swap(int* const p , int* const q)

{

int temp = *p;

*p = *q;

*q = temp;

}

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

Use the following template function to swap any two objects without using a temporary object:

template<class T> void swap(T& x, T& y){x^=y^=x^=y;}

Note that all objects passed to the function must provide an overload for the bitwise XOR assignment operator (^=) in order for this to work. However, primitive types such as int and char can be passed to this template function without the need for any additional code.

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

#include<iostream>

void swap(int& x, int& y)

{

x^=y^=x^=y;

}

int main()

{

int a=1, b=2;

std::cout<<"a="<<a<<", b="<<b<<std::endl;

swap(a,b);

std::cout<<"a="<<a<<", b="<<b<<std::endl;

return(0);

}

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

int main()

{

int a = 13, b = 29, c = 237

a ^= b; // a == 16

b ^= a; // b == 13

a ^= b; // a == 29

// You could stop here, since 'a' and 'b' values are now swapped, or you can continue with 'c' and 'b', as required.

c ^= b; // c == 224

b ^= c; // b == 237

c ^= b; // c == 13;

/* In short, 'a' got the previous value of 'b' , 'c' the value of 'b', and 'b' the value of 'c'.

To make it simpler

#define SWAP(X,Y) {X^=Y; Y^=X; X^=Y}

int main(void)

{

int a=37, b=32766;

SWAP(a,b); // nifty macro !

}

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

Please note the following coding for the same if the question is How do you write a program in C to swap two variables without using another variables? :-

void main()

{

int a,b;

printf("Enter a and b:", a,b);

scanf("%d %d",&a,&b);

a=a+b;

b=a-b;

a=a-b;

printf(" a=%d and b=%d",a,b);

}

By: Anil Kothiyal

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

void swap(int* x, int*y)

{

(*x)^=(*y)^=(*x)^=(*y);

}

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

to swap a and b we can use the following lines of code:

a=a+b;

b=a-b;

a=a-b;

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

a=6,b=5

a=a+b;/*11*/

b=a-b;/*11-5=6*/

a=a-b;/*11-6=5*/

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: C program to swap two variables without using third variable?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

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;


Third variable problem?

The third variable problem refers to the challenge of determining whether an observed relationship between two variables is direct or if it is influenced by a third variable that was not initially considered. This can lead to spurious or misleading conclusions if the third variable is not accounted for in the analysis. Control variables can help mitigate this issue by accounting for the potential influence of additional factors on the relationship between the two variables of interest.


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

Use list assignment i.e. for two variables $a, $b: ($a,$b) = ($b,$a)


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

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


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

a=a^b; b=a^b; 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 swap variables in PHP?

By using a third temporary variable. $tmp = $a; $a = $b; $b = $tmp;


Write a program for two variables without using third variable in c plus plus?

#include &lt;stdio.h&gt; int main (void) { char *first= "Hello"; int second = 12; printf ("first=%s, second=%d\n", first, second); return 0; }


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&lt;stdio.h&gt; main() { /*Without using third variable*/ int a,b,t; printf("Enter a:"); scanf("%d",&amp;a); printf("Enter b:"); scanf("%d",&amp;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); }


How do you write a program in C to swap two variables using the third variable?

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;


How can you devise a way to interchange two variables A and B without using a third variable or register using Exclusive OR?

A = A xor B B = A xor B A = A xor B in C... A^=B; B^=A; A^=B;


What is a z-axis?

A z-axis is the axis on a graph which is usually drawn as if vertical and usually shows the range of values of a variable dependent on two other variables or the third independent variable.