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 ?    1765 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 ?    547 Yes 164 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 184 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 ?    301 Yes 64 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 25 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 ?    94 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 77 No

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

Answer / gowtham

2 one will work correct

Is This Answer Correct ?    83 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 ?    45 Yes 11 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

void main() { int i=5; printf("%d",i++ + ++i); }

3 Answers  


main() { int i=-1; +i; printf("i = %d, +i = %d \n",i,+i); }

1 Answers  


main() { int i=-1,j=-1,k=0,l=2,m; m=i++&&j++&&k++||l++; printf("%d %d %d %d %d",i,j,k,l,m); }

1 Answers  


main() { char *p; int *q; long *r; p=q=r=0; p++; q++; r++; printf("%p...%p...%p",p,q,r); }

1 Answers  


int DIM(int array[]) { return sizeof(array)/sizeof(int ); } main() { int arr[10]; printf(“The dimension of the array is %d”, DIM(arr)); }

2 Answers   CSC,






What is your nationality?

1 Answers   GoDB Tech,


how to delete an element in an array

2 Answers   IBM,


main() { int c = 5; printf("%d", main||c); } a. 1 b. 5 c. 0 d. none of the above

2 Answers   HCL,


main() { extern int i; i=20; printf("%d",i); }

1 Answers   Value Labs,


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

1 Answers  


main() { char a[4]="HELL"; printf("%s",a); }

3 Answers   Wipro,


How will you print % character? a. printf(“\%”) b. printf(“\\%”) c. printf(“%%”) d. printf(“\%%”)

4 Answers   HCL,






Categories