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

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

1 Answers  


char *someFun() { char *temp = “string constant"; return temp; } int main() { puts(someFun()); }

1 Answers  


Write a program that produces these three columns sequence nos. using loop statement Sequence nos. Squared Squared + 5 1 1 6 2 4 9 3 9 14 4 16 21 5 25 30

1 Answers   GoDB,


main() { char not; not=!2; printf("%d",not); }

1 Answers  


write a c program to Create employee record by taking details like name, employee id, address and phone number. While taking the phone number, take either landline or mobile number. Ensure that the phone numbers of the employee are unique. Also display all the details

2 Answers   TCS,






int i=10; main() { extern int i; { int i=20; { const volatile unsigned i=30; printf("%d",i); } printf("%d",i); } printf("%d",i); }

1 Answers  


There are 21 people in a room. They have to form groups of 3 people each. How many combinations are possible? Write a C program to print the same.

1 Answers   TCS,


#include<stdio.h> main() { int i=1,j=2; switch(i) { case 1: printf("GOOD"); break; case j: printf("BAD"); break; } }

1 Answers  


How to palindrom string in c language?

6 Answers   Google,


main() { printf("\nab"); printf("\bsi"); printf("\rha"); }

3 Answers  


main( ) { char *q; int j; for (j=0; j<3; j++) scanf(“%s” ,(q+j)); for (j=0; j<3; j++) printf(“%c” ,*(q+j)); for (j=0; j<3; j++) printf(“%s” ,(q+j)); }

1 Answers  


main() { int c[ ]={2.8,3.4,4,6.7,5}; int j,*p=c,*q=c; for(j=0;j<5;j++) { printf(" %d ",*c); ++q; } for(j=0;j<5;j++){ printf(" %d ",*p); ++p; } }

2 Answers   CSS, Wipro,


Categories