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 ? 1768 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 165 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 185 No

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

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

 Is This Answer Correct ? 302 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

Display the time of the system and display the right time of the other country

Write a routine to draw a circle (x ** 2 + y ** 2 = r ** 2) without making use of any floating point computations at all.

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); }

print a semicolon using Cprogram without using a semicolon any where in the C code in ur program!!

respected sir, i did my MCA in 2013 when i am going to attend to an interview i was asked about my project how will i explain my project could please help me in this and my project title is "Social Networking Site For Social Responsibility"

main() { char *p = "hello world"; p = 'H'; printf("%s", p); } a. Runtime error. b. “Hello world” c. Compile error d. “hello world”

Write a C program to print look and say sequence? For example if u get the input as 1 then the sequence is 11 21 1211 111221 312211 12112221 .......(it counts the no. of 1s,2s etc which is in successive order) and this sequence is used in run-length encoding.

void main() { static int i; while(i<=10) (i>2)?i++:i--; printf(“%d”, i); }

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); }

#define clrscr() 100 main() { clrscr(); printf("%d\n",clrscr()); }

main() { char not; not=!2; printf("%d",not); }

typedef struct error{int warning, error, exception;}error; main() { error g1; g1.error =1; printf("%d",g1.error); }

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