answersLogoWhite

0


Best Answer

void main() { int a,b; clrscr(); printf("\n\n\t\tenter any two nos..."); scanf("%d%d",&a,&b); a=a+b; b=a-b; a=a-b; printf("\n\n\t\tvalue of a=",a); printf("\n\n\t\tvalue of b=",b); getch(); }

User Avatar

Wiki User

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

Wiki User

11y ago

Use the bitwise XOR assignment operator (^=) three times, alternating the operands each time.

Note that the operands must be distinct -- they may be the same value, but they must be different variables (in completely different memory locations). XORing a variable with itself yields the value zero, so if x=1, x^=x would make x=0, thus the "swap" fails.

The following code demonstrates the algorithm.

#include <iostream>

int main()

{

int x = 42;

int y = 67;

printf( "Before:\tx=%d, y=%d\n", x, y );

x ^= y;

y ^= x;

x ^= y;

printf( "After:\tx=%d, y=%d\n", x, y );

return( 0 );

}

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

a=b+a;

b=a-b;

a=a-b;

with 3rd variable

If a=10

b=20

c=a; (now c=10 and a is empty)

a=b; ( b's value assign to a=20)

b=c; (now c assign value to b=10)

now it is swapped

a=20

b=10

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

Let us take a=40,b=50.Now after swapping,we should get the output as a=50,b=40.

main()

{

int a=40,b=50;

a=a+b;

b=a-b;

a=a-b;

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

}

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

In C++, the coding is :

#include <iostream.h>

#include <conio.h>

void main()

{

int a,b;

cout<<"type the value of a and b ";

cin>>a>>b;

a=a+b;

b=a-b;

a=a-b;

cout<<"after swapping the values are :";

cout<<a<<"\t"<<b<<"\n";

getch();

}

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

1)

num1 = num1 + num2;

num2 = num1 - num2;

num1 = num1 - num2;

Enter Number 1: 20

Enter Number 2:

10

After swapping, num1=10 and num2=20

2)

x -= y;

y += x; // y becomes x

x = (y - x); // x becomes y

3)

x = x*y;

y = x / y;

x = x / y;

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you swap 2 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;


How do you swap variables in PHP?

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


What is the advantage of swapping two variables without using 3 variable?

Nothing. Never do that.


What is the difference between controlling variables and using controls?

Controlling variables is when you make sure that only one variable is being tested at a time and that there are not other variables that will make your results unclear. Using a control is when you do a trial without the variable to see what the normal results are.


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;


Swap logic without using third variable?

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


What is the Algorithm for exchanging values of two variables?

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;


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;


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)


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 access the static variable and static methods of a class?

In java we access static variables and static methods without creating objects. i.e.,we can access directly by using classname we can also access static variables and static methods by using objects which are created by using class where the static variables and static methods are available


Swapping without third variable?

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