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

int swap(int *a,int *b) { *a=*a+*b;*b=*a-*b;*a=*a-*b; } main() { int x=10,y=20; swap(&x,&y); printf("x= %d y = %d\n",x,y); }

1 Answers  


write a program in c to merge two array

2 Answers  


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

3 Answers   CSC,


Given an array of characters which form a sentence of words, give an efficient algorithm to reverse the order of the words (not characters) in it.

9 Answers   Microsoft,


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

2 Answers   HCL,






#include<stdio.h> main() { char s[]={'a','b','c','\n','c','\0'}; char *p,*str,*str1; p=&s[3]; str=p; str1=s; printf("%d",++*p + ++*str1-32); }

2 Answers   CNSI,


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  


void main() { int i=i++,j=j++,k=k++; printf(ā€œ%d%d%dā€,i,j,k); }

1 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() { char c; int i = 456; clrscr(); c = i; printf("%d", c); } a. 456 b. -456 c. random number d. none of the above

3 Answers   BrickRed, HCL,


You are given any character string. Find the number of sets of vowels that come in the order of aeiou in the given string. For eg., let the given string be DIPLOMATIC. The answer returned must be "The number of sets is 2" and "The sets are "IO and AI". Vowels that form a singleton set must be neglected. Try to post the program executable in gcc or g++ or in java.

3 Answers  


Code for 1>"ascii to string" 2>"string to ascii"

1 Answers   Aricent, Global Logic,


Categories