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

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  


#include"math.h" void main() { printf("Hi everybody"); } if <stdio.h> will be included then this program will must compile, but as we know that when we include a header file in "" then any system defined function find its defination from all the directrives. So is this code of segment will compile? If no then why?

2 Answers  


#include<stdio.h> main() { struct xx { int x=3; char name[]="hello"; }; struct xx *s; printf("%d",s->x); printf("%s",s->name); }

3 Answers   Hexaware,


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

5 Answers   HCL,


Finding a number multiplication of 8 with out using arithmetic operator

8 Answers   Eyeball, NetApp,






#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  


How will u find whether a linked list has a loop or not?

8 Answers   Microsoft,


write a c-program to display the time using FOR loop

3 Answers   HCL,


Set up procedure for generating a wire frame display of a polyhedron with the hidden edges of the object drawn with dashed lines

0 Answers   IBM,


main() { char str1[] = {‘s’,’o’,’m’,’e’}; char str2[] = {‘s’,’o’,’m’,’e’,’\0’}; while (strcmp(str1,str2)) printf(“Strings are not equal\n”); }

1 Answers  


main() { char *p; p="Hello"; printf("%c\n",*&*p); }

1 Answers  


main() { char *p="hai friends",*p1; p1=p; while(*p!='\0') ++*p++; printf("%s %s",p,p1); }

3 Answers  


Categories