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

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 ?    1770 Yes 223 No

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

Answer / guest

use xor to swap

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

Is This Answer Correct ?    550 Yes 165 No

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

Answer / somasekhar

Ans is :

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

Is This Answer Correct ?    536 Yes 185 No

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

Answer / kiran

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

Is This Answer Correct ?    302 Yes 65 No

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

Answer / mohit

x = x + y;

y = x - y;

x = x - y;

Is This Answer Correct ?    113 Yes 26 No

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

Answer / bruce tuskey

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 ?    95 Yes 21 No

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

Answer / partha

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

Is This Answer Correct ?    119 Yes 78 No

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

Answer / gowtham

2 one will work correct

Is This Answer Correct ?    84 Yes 45 No

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

Answer / pavan.

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 ?    46 Yes 12 No

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

Answer / yash

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

Is This Answer Correct ?    44 Yes 17 No

Post New Answer

More C Code Interview Questions

main() { char *a = "Hello "; char *b = "World"; clrscr(); printf("%s", strcat(a,b)); } a. Hello b. Hello World c. HelloWorld d. None of the above

3 Answers   HCL,


what will be the output of this program? void main() { int a[]={5,10,15}; int i=0,num; num=a[++i] + ++i +(++i); printf("%d",num); }

3 Answers   Wipro,


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

1 Answers  


abcdedcba abc cba ab ba a a

2 Answers  


main() { int i=10; void pascal f(int,int,int); f(i++,i++,i++); printf(" %d",i); } void pascal f(integer :i,integer:j,integer :k) { write(i,j,k); }

1 Answers  






how to concatenate the two strings

1 Answers  


main() { static int var = 5; printf("%d ",var--); if(var) main(); }

1 Answers  


#if something == 0 int some=0; #endif main() { int thing = 0; printf("%d %d\n", some ,thing); }

1 Answers  


main() { int i; printf("%d",scanf("%d",&i)); // value 10 is given as input here }

2 Answers   IBM,


main() { char string[]="Hello World"; display(string); } void display(char *string) { printf("%s",string); }

2 Answers   Wipro,


why array index always strats wuth zero?

2 Answers  


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

1 Answers  


Categories