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 ?..

Hi this question was asked in my interview.
Ans is :

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

Any other solution?

 Is This Answer Correct ? 1765 Yes 223 No

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

use xor to swap

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

 Is This Answer Correct ? 547 Yes 164 No

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

Ans is :

a=a*b;
b=a/b;
a=a/b;

 Is This Answer Correct ? 536 Yes 184 No

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

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

 Is This Answer Correct ? 301 Yes 64 No

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

x = x + y;

y = x - y;

x = x - y;

 Is This Answer Correct ? 113 Yes 25 No

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

Only the Xor answer (#2) is correct (in cases where the
variables are the same size). With all the other answers,
you could run into over/under flow problems.
A = 01111111
B = 01111101

A = A^B = 00000010
B = A^B = 01111111 (Original A)
A = A^B = 01111101 (Original B)

 Is This Answer Correct ? 94 Yes 21 No

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

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

 Is This Answer Correct ? 119 Yes 77 No

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

2 one will work correct

 Is This Answer Correct ? 83 Yes 45 No

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

void main()
{
int a,b;
printf("Enter two number : ");
scanf("%d%d",&a,&b);
a=a+b;
b=a-b;
a=a-b;
printf("The swap number is : %d %d",a,b);
getch();
}

 Is This Answer Correct ? 45 Yes 11 No

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

Answer no 12 is wrong. the logic fails when a=0;

 Is This Answer Correct ? 44 Yes 17 No

More C Code Interview Questions

What are the files which are automatically opened when a C file is executed?

#include<stdio.h> int main() { int x=2,y; y=++x*x++*++x; printf("%d",y); } Output for this program is 64. can you explain how this output is come??

write a c program to Reverse a given string using string function and also without string function

main() { int *ptr=(int*)malloc(sizeof(int)); *ptr=4; printf("%d",(*ptr)+++*ptr++); }

Who could write how to find a prime number in dynamic array?

program to find the roots of a quadratic equation

char *someFun1() { char temp[ ] = “string"; return temp; } char *someFun2() { char temp[ ] = {‘s’, ‘t’,’r’,’i’,’n’,’g’}; return temp; } int main() { puts(someFun1()); puts(someFun2()); }

main() { unsigned char i=0; for(;i>=0;i++) ; printf("%d\n",i); }

int i; main(){ int t; for ( t=4;scanf("%d",&i)-t;printf("%d\n",i)) printf("%d--",t--); } // If the inputs are 0,1,2,3 find the o/p

main() { { unsigned int bit=256; printf("%d", bit); } { unsigned int bit=512; printf("%d", bit); } } a. 256, 256 b. 512, 512 c. 256, 512 d. Compile error

main() { extern out; printf("%d", out); } int out=100;

#define a 10 int main() { printf("%d..",a); foo(); printf("%d..",a); return 0; } void foo() { #undef a #define a 50 }

Categories
• C Code (421)
• C++ Code (127)
• VC++ Code (17)
• Java Code (84)
• Dot Net Code (114)
• Visual Basic Code (11)
• Programming Code AllOther (62)