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

struct point { int x; int y; }; struct point origin,*pp; main() { pp=&origin; printf("origin is(%d%d)\n",(*pp).x,(*pp).y); printf("origin is (%d%d)\n",pp->x,pp->y); }

1 Answers  


Write a single line c expression to delete a,b,c from aabbcc

2 Answers   Microsoft,


abcdedcba abc cba ab ba a a

2 Answers  


main() { extern out; printf("%d", out); } int out=100;

1 Answers  


void pascal f(int i,int j,int k) { printf(“%d %d %d”,i, j, k); } void cdecl f(int i,int j,int k) { printf(“%d %d %d”,i, j, k); } main() { int i=10; f(i++,i++,i++); printf(" %d\n",i); i=10; f(i++,i++,i++); printf(" %d",i); }

1 Answers  


What is wrong with the following code? int *foo() { int *s = malloc(sizeof(int)100); assert(s != NULL); return s; }

1 Answers  


what will be the position of the file marker? a: fseek(ptr,0,SEEK_SET); b: fseek(ptr,0,SEEK_CUR);

2 Answers  


main(int argc, char *argv[]) { (main && argc) ? main(argc-1, NULL) : return 0; } a. Runtime error. b. Compile error. Illegal syntax c. Gets into Infinite loop d. None of the above

4 Answers   HCL, LG,


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

1 Answers  


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,


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 to return a multiple value from a function?

2 Answers   Wipro,


Categories