How to swap two variables, without using third variable ?

Answers were Sorted based on User's Feedback



How to swap two variables, without using third variable ?..

Answer / p.muthukumar

b=a-b;
a=a-b;
b=a+b;

Is This Answer Correct ?    101 Yes 86 No

How to swap two variables, without using third variable ?..

Answer / 3uggy3oy

Answer #16 is totally wrong it fails
when x>y and many other situation.

Is This Answer Correct ?    22 Yes 11 No

How to swap two variables, without using third variable ?..

Answer / alex

When swapping integers or characters, use:
a^=b;
b^=a;
a^=b;
When swapping floats or doubles use:
a+=b;
b=a-b;
a=a-b;
When swapping strings use:
a=(char*)((int)a^(int)b);
b=(char*)((int)a^(int)b);
a=(char*)((int)a^(int)b);
This works by modifying the address values of the pointer.

Is This Answer Correct ?    12 Yes 2 No

How to swap two variables, without using third variable ?..

Answer / shubham

NOne of the answer is correct except the 2ND one.....Please
don't give wrong answers.

Is This Answer Correct ?    32 Yes 23 No

How to swap two variables, without using third variable ?..

Answer / hanmanth reddy

a=a+b-(b=a);

Is This Answer Correct ?    27 Yes 19 No

How to swap two variables, without using third variable ?..

Answer / bhaskar.mantrala

void main()
{
int a,b;
clrscr();
printf("\n Enter the values of a and b ");
scanf(" %d %d ", &a,&b);
a=a*b;
b=a/b;
a=a/b;
printf("\n \n After swapping ----> a = %d \t b = %d",a,b);
getch();
}

Is This Answer Correct ?    9 Yes 3 No

How to swap two variables, without using third variable ?..

Answer / trinath somarouthu

using X-OR
#define SWAP(x,y) x^=y^=x^=y

x = x ^ y --> x^=y -- (1)
y = y ^ x --> y^=x -- (2)
x = x ^ y --> x^=y -- (3)

(3) in (2) --> y^=x^=y -- (4)
(4) in (1) --> x^=y^=x^=y -- :-)

all togeather, he single line code

#define SWAP(x,y) x^=y^=x^=y

Is This Answer Correct ?    17 Yes 13 No

How to swap two variables, without using third variable ?..

Answer / sunil kharat

a^=b^=a^=b;

try it...
it works. one line solution

Is This Answer Correct ?    4 Yes 0 No

How to swap two variables, without using third variable ?..

Answer / sujata

i think first is right.

Is This Answer Correct ?    8 Yes 5 No

How to swap two variables, without using third variable ?..

Answer / perl guru

($a,$b)=($b,$a)
....this is how we do in PERL....Ha Ha Ha pretty cool !!!!

And BELIEVE me it works not only for NUMBERS but also for
STRINGS, ARRAYS or any Data Structure or any garbage
value....this is mine Challenge.....

None of the any programming language can come close to PERL
in this much of SIMPLICITY and ROBUSTNESS....just one line
does the Magic....

Again I would say the Question itself is very silly one "How
to swap two variables, without using third variable
?"....and I see alot of stupid Answers posted here....

Everyone posting the answer is assuming that Question is
about swapping INTEGER NUMBERS....I would like to ask what
if I provide FLOATING POINT NUMBERS, NEGATIVE NUMBERS, REAL
NUMBERS, and surely it does not work for STRINGS....

That is why I say "Perl is immensely powerful. If you think
something can't be done, the problem is likely to be it is
beyond your ability, not that of Perl."

Welcome to the world of PERL....its more precious than PEARL....

Is This Answer Correct ?    4 Yes 1 No

Post New Answer

More C Code Interview Questions

How to use power function under linux environment.eg : for(i=1;i<=n;i++){ pow(-1,i-1)} since it alerts undefined reference to 'pow'.

2 Answers  


3) Int Matrix of certain size was given, We had few valu= es in it like this. =97=97=97=97=97=97=97=97=97=97=97 1 = | 4 | | 5 | &= nbsp; | 45 =97=97=97=97=97=97=97=97=97=97=97 &n= bsp; | 3 | 3 | 5 | = | 4 =97=97=97=97=97=97=97=97=97=97=97 34 |&nbs= p; 3 | 3 | | 12 | &= nbsp; =97=97=97=97=97=97=97=97=97=97=97 3 | &nbs= p; | 3 | 4 | = | 3 =97=97=97=97=97=97=97=97=97=97=97 3 | = ; | | | = ; 3 | =97=97=97=97=97=97=97=97=97=97=97 &= nbsp; | | 4 | = ; | 4 | 3 We w= ere supposed to move back all the spaces in it at the end. Note: = If implemented this prog using recursion, would get higher preference.

0 Answers   RoboSoft,


main() { int c=- -2; printf("c=%d",c); }

1 Answers   TCS,


Which version do you prefer of the following two, 1) printf(“%s”,str); // or the more curt one 2) printf(str);

1 Answers  


To Write a C program to remove the repeated characters in the entered expression or in entered characters(i.e) removing duplicates.

19 Answers   Amazon, BITS, Microsoft, Syncfusion, Synergy, Vector,


1. const char *a; 2. char* const a; 3. char const *a; -Differentiate the above declarations.

3 Answers  


main() { float f=5,g=10; enum{i=10,j=20,k=50}; printf("%d\n",++k); printf("%f\n",f<<2); printf("%lf\n",f%g); printf("%lf\n",fmod(f,g)); }

1 Answers  


main() { char *cptr,c; void *vptr,v; c=10; v=0; cptr=&c; vptr=&v; printf("%c%v",c,v); }

1 Answers  


Question: We would like to design and implement a programming solution to the reader-writer problem using semaphores in C language under UNIX. We assume that we have three readers and two writers processes that would run concurrently. A writer is to update (write) into one memory location (let’s say a variable of type integer named temp initialized to 0). In the other hand, a reader is to read the content of temp and display its content on the screen in a formatted output. One writer can access the shared data exclusively without the presence of other writer or any reader, whereas, a reader may access the shared memory for reading with the presence of other readers (but not writers).

1 Answers  


Given an array of characters which form a sentence of words, give an efficient algorithm to reverse the order of the words (not characters) in it.

2 Answers   Wipro,


#include<stdio.h> main() { FILE *ptr; char i; ptr=fopen("zzz.c","r"); while((i=fgetch(ptr))!=EOF) printf("%c",i); }

1 Answers  


main() { int i=4,j=7; j = j || i++ && printf("YOU CAN"); printf("%d %d", i, j); }

1 Answers  


Categories