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() { int *ptr=(int*)malloc(sizeof(int)); *ptr=4; printf("%d",(*ptr)+++*ptr++); }

3 Answers   GNITC,


All the combinations of prime numbers whose sum gives 32

1 Answers   HHH,


how to concatenate the two strings

1 Answers  


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

1 Answers   Wipro,


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

2 Answers   Wipro,






struct point { int x; int y; }; struct point origin,*pp; main() { pp=&origin; printf("origin is(%d%d)\n",(*pp).x,(*pp).y); printf("origin is (%d%d)\n",pp->x,pp->y); }

1 Answers  


find simple interest & compund interest

2 Answers  


main() { register int a=2; printf("Address of a = %d",&a); printf("Value of a = %d",a); }

3 Answers  


Predict the Output: int main() { int *p=(int *)2000; scanf("%d",2000); printf("%d",*p); return 0; } if input is 20 ,what will be print

2 Answers  


Finding a number which was log of base 2

1 Answers   NetApp,


what will be the position of the file marker? a: fseek(ptr,0,SEEK_SET); b: fseek(ptr,0,SEEK_CUR);

2 Answers  


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

2 Answers   IBM,


Categories