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

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,


main() { int x=5; clrscr(); for(;x==0;x--) { printf("x=%d\nā€", x--); } } a. 4, 3, 2, 1, 0 b. 1, 2, 3, 4, 5 c. 0, 1, 2, 3, 4 d. none of the above

3 Answers   HCL,


Write a C program to print look and say sequence? For example if u get the input as 1 then the sequence is 11 21 1211 111221 312211 12112221 .......(it counts the no. of 1s,2s etc which is in successive order) and this sequence is used in run-length encoding.

1 Answers  


Write a c program to search an element in an array using recursion

1 Answers   Wipro,


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

1 Answers  






Is the following code legal? typedef struct a aType; struct a { int x; aType *b; };

1 Answers  


how to check whether a linked list is circular.

11 Answers   Microsoft,


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

1 Answers  


write a program in c to merge two array

2 Answers  


main() { int i = 100; clrscr(); printf("%d", sizeof(sizeof(i))); } a. 2 b. 100 c. 4 d. none of the above

5 Answers   HCL,


main() { int y; scanf("%d",&y); // input given is 2000 if( (y%4==0 && y%100 != 0) || y%100 == 0 ) printf("%d is a leap year"); else printf("%d is not a leap year"); }

1 Answers  


main() { int i; clrscr(); printf("%d", &i)+1; scanf("%d", i)-1; } a. Runtime error. b. Runtime error. Access violation. c. Compile error. Illegal syntax d. None of the above

1 Answers   HCL,


Categories