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

x = y | (y=x)-x;

&
a=a*b/(b=a); provided either of a or b is not 0

Is This Answer Correct ?    1 Yes 0 No

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

Answer / adi

answer no. 3 wont work in case of b=0!!!

Is This Answer Correct ?    1 Yes 0 No

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

Answer / mrx

In assembly language you could use "xchg" instruction witch
swaps the two variables in one instruction.

/* swaps %dest <-> %source */
xchg %dest, %source


/* inline assambly example for C language */
void swap(int register *x, register int *y)
{
asm("xchg %0, %1;" \
: "=r"(*x), "=r"(*y)
: "0"(*x), "1"(*y)
);
}

This is the fastest method you can achieve in hand
optimization. I think C compiler will do the same trick if
possible.

Is This Answer Correct ?    1 Yes 0 No

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

Answer / simhachalam lakkakula

Answer 4 get fail for the following values

Before: a= 219455078, b= -747788332
After: a= -875186568, b= 1

Is This Answer Correct ?    1 Yes 0 No

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

Answer / simhachalam lakkakula

Answer 4
Logic:
a=a*b;
b=a/b;
a=a/b;

get fail for the following values

Before: a= 219455078, b= -747788332
After: a= -875186568, b= 1

Is This Answer Correct ?    1 Yes 0 No

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

Answer / suresh bhandari

---------------------
a=a*b
b=a/b
a=a/b
---------------------
a=a+b
b=a-b
a=a-b
----------------------
a = a Xor b
b= a Xor b
a= a Xor b
----------------------
all above three methods are correct, i hv checked...

Is This Answer Correct ?    2 Yes 1 No

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

Answer / d

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


is this wrong?

Is This Answer Correct ?    14 Yes 14 No

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

Answer / rahul

I am new in C/C++.
How to write the program for second answer. Does it
automatically convert the numbers in binary by using ^ sign.

:}

Is This Answer Correct ?    0 Yes 0 No

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

Answer / devarajan.k

a[12]=name
b[20]=name
Here the size 12 & 25 has no effect hence we can change
the size
a[]=name
b[]=name

Is This Answer Correct ?    0 Yes 0 No

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

Answer / poojamnagal

Num1 = 5
Num2 = 7

Num1 = 5+7 = 12
Num2 = 12- 7 = 5
Num1 = 12-5 = 7

So, Now Num1 = 7
Num2 = 5

Is This Answer Correct ?    2 Yes 2 No

Post New Answer

More C Code Interview Questions

main() { char *p; p="%d\n"; p++; p++; printf(p-2,300); }

1 Answers  


What are segment and offset addresses?

2 Answers   Infosys,


why is printf("%d %d %d",i++,--i,i--);

4 Answers   Apple, Cynity, TCS,


write a program to count the number the same (letter/character foreg: 's') in a given sentence.

2 Answers  


main() { int i=300; char *ptr = &i; *++ptr=2; printf("%d",i); }

4 Answers   CSC,






#include<stdio.h> main() { register i=5; char j[]= "hello"; printf("%s %d",j,i); }

2 Answers  


#include<stdio.h> #include<conio.h> void main() { int a=(1,2,3,(1,2,3,4); switch(a) { printf("ans:"); case 1: printf("1");break; case 2: printf("2");break; case 3: printf("1");break; case 4: printf("4");break; printf("end"); } getch(); }

0 Answers  


#include<stdio.h> main() { struct xx { int x; struct yy { char s; struct xx *p; }; struct yy *q; }; }

2 Answers  


C program to print magic square of order n where n > 3 and n is odd

2 Answers   Accenture,


main() { char *p="GOOD"; char a[ ]="GOOD"; printf("\n sizeof(p) = %d, sizeof(*p) = %d, strlen(p) = %d", sizeof(p), sizeof(*p), strlen(p)); printf("\n sizeof(a) = %d, strlen(a) = %d", sizeof(a), strlen(a)); }

1 Answers  


hello sir,is there any function in C that can calculate number of digits in an int type variable,suppose:int a=123; 3 digits in a.what ll b answer?

6 Answers  


How we print the table of 3 using for loop in c programing?

7 Answers  


Categories