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() { int i; clrscr(); for(i=0;i<5;i++) { printf("%d\n", 1L << i); } } a. 5, 4, 3, 2, 1 b. 0, 1, 2, 3, 4 c. 0, 1, 2, 4, 8 d. 1, 2, 4, 8, 16

4 Answers   HCL,


Finding a number which was log of base 2

1 Answers   NetApp,


Write a C program to print ‘Campus Force training’ without using even a single semicolon in the program.

3 Answers   Wipro,


main() { if ((1||0) && (0||1)) { printf("OK I am done."); } else { printf("OK I am gone."); } } a. OK I am done b. OK I am gone c. compile error d. none of the above

5 Answers   HCL,


Write, efficient code for extracting unique elements from a sorted list of array. e.g. (1, 1, 3, 3, 3, 5, 5, 5, 9, 9, 9, 9) -> (1, 3, 5, 9).

13 Answers   Intel, Microsoft, TCS,






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

1 Answers  


Given a spherical surface, write bump-mapping procedure to generate the bumpy surface of an orange

0 Answers  


To Write a C program to remove the repeated characters in the entered expression or in entered characters(i.e) removing duplicates. String contains only lowercase characters ['a'-'z']

0 Answers  


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

2 Answers  


void main() { char far *farther,*farthest; printf("%d..%d",sizeof(farther),sizeof(farthest)); }

2 Answers  


What are the following notations of defining functions known as? i. int abc(int a,float b) { /* some code */ } ii. int abc(a,b) int a; float b; { /* some code*/ }

1 Answers  


String reverse with time complexity of n/2 with out using temporary variable.

10 Answers   NetApp, Symantec,


Categories