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 / marutikutre

x = y | (y=x)-x;

&
a=a*b/(b=a); provided either of a or b is not 0

Is This Answer Correct ?    1 Yes 0 No

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

Answer / adi

answer no. 3 wont work in case of b=0!!!

Is This Answer Correct ?    1 Yes 0 No

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

Answer / mrx

In assembly language you could use "xchg" instruction witch
swaps the two variables in one instruction.

/* swaps %dest <-> %source */
xchg %dest, %source


/* inline assambly example for C language */
void swap(int register *x, register int *y)
{
asm("xchg %0, %1;" \
: "=r"(*x), "=r"(*y)
: "0"(*x), "1"(*y)
);
}

This is the fastest method you can achieve in hand
optimization. I think C compiler will do the same trick if
possible.

Is This Answer Correct ?    1 Yes 0 No

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

Answer / simhachalam lakkakula

Answer 4 get fail for the following values

Before: a= 219455078, b= -747788332
After: a= -875186568, b= 1

Is This Answer Correct ?    1 Yes 0 No

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

Answer / simhachalam lakkakula

Answer 4
Logic:
a=a*b;
b=a/b;
a=a/b;

get fail for the following values

Before: a= 219455078, b= -747788332
After: a= -875186568, b= 1

Is This Answer Correct ?    1 Yes 0 No

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

Answer / suresh bhandari

---------------------
a=a*b
b=a/b
a=a/b
---------------------
a=a+b
b=a-b
a=a-b
----------------------
a = a Xor b
b= a Xor b
a= a Xor b
----------------------
all above three methods are correct, i hv checked...

Is This Answer Correct ?    2 Yes 1 No

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

Answer / d

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


is this wrong?

Is This Answer Correct ?    14 Yes 14 No

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

Answer / rahul

I am new in C/C++.
How to write the program for second answer. Does it
automatically convert the numbers in binary by using ^ sign.

:}

Is This Answer Correct ?    0 Yes 0 No

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

Answer / devarajan.k

a[12]=name
b[20]=name
Here the size 12 & 25 has no effect hence we can change
the size
a[]=name
b[]=name

Is This Answer Correct ?    0 Yes 0 No

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

Answer / xyz

Congrats to all guys who have tried this.Everything u post
here is correct

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More C Code Interview Questions

Code for 1>"ascii to string" 2>"string to ascii"

1 Answers   Aricent, Global Logic,


#define clrscr() 100 main() { clrscr(); printf("%d\n",clrscr()); }

2 Answers  


given integer number,write a program that displays the number as follows: First line :all digits second line : all except the first digit . . . . Last line : the last digit

8 Answers  


main() { static int a[3][3]={1,2,3,4,5,6,7,8,9}; int i,j; static *p[]={a,a+1,a+2}; for(i=0;i<3;i++) { for(j=0;j<3;j++) printf("%d\t%d\t%d\t%d\n",*(*(p+i)+j), *(*(j+p)+i),*(*(i+p)+j),*(*(p+j)+i)); } }

1 Answers  


Write a routine that prints out a 2-D array in spiral order

3 Answers   Microsoft,






Write a prog to accept a given string in any order and flash error if any of the character is different. For example : If abc is the input then abc, bca, cba, cab bac are acceptable, but aac or bcd are unacceptable.

5 Answers   Amazon, Microsoft,


how many processes will gate created execution of -------- fork(); fork(); fork(); -------- Please Explain... Thanks in advance..!

8 Answers   GATE,


write a c program to print magic square of order n when n>3 and n is odd?

1 Answers   HCL,


main() { char *str1="abcd"; char str2[]="abcd"; printf("%d %d %d",sizeof(str1),sizeof(str2),sizeof("abcd")); }

1 Answers  


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

3 Answers  


main( ) { int a[ ] = {10,20,30,40,50},j,*p; for(j=0; j<5; j++) { printf(“%d” ,*a); a++; } p = a; for(j=0; j<5; j++) { printf(“%d ” ,*p); p++; } }

1 Answers  


void main() { char ch; for(ch=0;ch<=127;ch++) printf(“%c %d \n“, ch, ch); }

1 Answers  


Categories