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

only #2 is d right answer......XOR yields...perfect answers.....

Is This Answer Correct ?    4 Yes 1 No

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

Answer / balasubramanian ganapthi

a=a+b-(a=b);

This is the example by using only one line to swap the two
variables without using the third variable.

Is This Answer Correct ?    4 Yes 2 No

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

Answer / ashesh anand

Thanks Guys....
#2 is absolutely right...

Is This Answer Correct ?    3 Yes 1 No

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

Answer / xeon

To note on the answer about Perl, it uses a third variable
and just doesn't tell you. The C++ equivalent is:

//Our data
class exampleclass {};
exampleclass classa, classb;
int inta, intb;
float floata, floatb;

//Our swap function
template<class T>
void swap(T *a, T *b) {
T temp = *a;
a = b;
*b = temp;
}

//Our use of the swap function
swap<classexample>(classa, classb);
swap<int>(inta, intb);
swap<float>(floata, floatb);

And also note, Perl is an interpreted (Script) language, so
though it is simple and robust, it is less efficient than a
language that is compiled.

Is This Answer Correct ?    2 Yes 0 No

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

Answer / dipak

#51 is perfect solution.

Is This Answer Correct ?    4 Yes 2 No

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

Answer / raja sekhar sharma

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

Is This Answer Correct ?    4 Yes 2 No

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

Answer / ankit goel

Sorry i dont know..??
only 1st ans is write......
so give me positive marking....
Thank You...

Is This Answer Correct ?    8 Yes 7 No

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

Answer / pallabi

if any one of the variables eithet A or B is negative or
else both r negatibe then how logic will vary in cobol?

Is This Answer Correct ?    1 Yes 0 No

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

Answer / indu b

a=10;
b=20;
a=a+b;
b=a-b;
a=a-b;
a=20,b=10;

Is This Answer Correct ?    4 Yes 3 No

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

Answer / sriram

Hey guys, what about swaping variables contains string
values? All of your solutions will suck... Try this and swap
any datatype without third variable... Happy Sensible Coding..

$v = 'sriram';
$u = 'lakshmi';

$v .= $u;
$u = substr($v,0,(strlen($v) - strlen($u)));
$v = substr($v,(strlen($v) - strlen($u)-1), strlen($v));

echo 'u = ' . $u .'<br>';
echo 'v = ' . $v;

Is This Answer Correct ?    1 Yes 0 No

Post New Answer

More C Code Interview Questions

Is the following statement a declaration/definition. Find what does it mean? int (*x)[10];

1 Answers  


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

4 Answers   CSC,


How to use power function under linux environment.eg : for(i=1;i<=n;i++){ pow(-1,i-1)} since it alerts undefined reference to 'pow'.

2 Answers  


How do you sort a Linked List (singly connected) in O(n) please mail to pawan.10k@gmail.com if u can find an anser...i m desperate to knw...

6 Answers   Microsoft, MSD, Oracle,


main() { static int var = 5; printf("%d ",var--); if(var) main(); }

1 Answers  


how to programme using switch statements and fuctions, a programme that will output two even numbers, two odd numbers and two prime numbers of the users chioce.

0 Answers   Mbarara University of Science and Technology,


const int perplexed = 2; #define perplexed 3 main() { #ifdef perplexed #undef perplexed #define perplexed 4 #endif printf("%d",perplexed); } a. 0 b. 2 c. 4 d. none of the above

1 Answers   emc2, HCL,


main() { clrscr(); } clrscr();

2 Answers  


main(){ char a[100]; a[0]='a';a[1]]='b';a[2]='c';a[4]='d'; abc(a); } abc(char a[]){ a++; printf("%c",*a); a++; printf("%c",*a); }

2 Answers  


struct Foo { char *pName; char *pAddress; }; main() { struct Foo *obj = malloc(sizeof(struct Foo)); clrscr(); obj->pName = malloc(100); obj->pAddress = malloc(100); strcpy(obj->pName,"Your Name"); strcpy(obj->pAddress, "Your Address"); free(obj); printf("%s", obj->pName); printf("%s", obj->pAddress); } a. Your Name, Your Address b. Your Address, Your Address c. Your Name Your Name d. None of the above

2 Answers   HCL,


why the range of an unsigned integer is double almost than the signed integer.

1 Answers  


Is the following code legal? struct a { int x; struct a *b; }

2 Answers  


Categories