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

How do you create a really large matrix (i.e. 3500x3500) in C without having the program crash? I can only reach up to 2500. It must have something to do with lack of memory. Please help!

1 Answers  


#include <stdio.h> int main(void) { int a=4, b=2; a=b<<a+b>>2 ; printf("%d",a); return 0; }

0 Answers   Student,


int a=1; printf("%d %d %d",a++,a++,a); need o/p in 'c' and what explanation too

1 Answers  


main() { int a=10,*j; void *k; j=k=&a; j++; k++; printf("\n %u %u ",j,k); }

1 Answers  


main() { char *cptr,c; void *vptr,v; c=10; v=0; cptr=&c; vptr=&v; printf("%c%v",c,v); }

1 Answers  






Write a Program in 'C' To Insert a Unique Number Only. (Hint: Just Like a Primary Key Numbers In Database.) Please Some One Suggest Me a Better Solution for This question ??

0 Answers   Home,


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  


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

8 Answers   Microsoft,


void main() { static int i=i++, j=j++, k=k++; printf(“i = %d j = %d k = %d”, i, j, k); }

3 Answers  


how many processes will gate created execution of -------- fork(); fork(); fork(); -------- Please Explain... Thanks in advance..!

8 Answers   GATE,


main() { int i=5; printf("%d%d%d%d%d%d",i++,i--,++i,--i,i); }

7 Answers  


What are the files which are automatically opened when a C file is executed?

1 Answers  


Categories