How to swap two variables, without using third variable ?
Answers were Sorted based on User's Feedback
Answer / 3uggy3oy
Answer #16 is totally wrong it fails
when x>y and many other situation.
Is This Answer Correct ? | 22 Yes | 11 No |
Answer / alex
When swapping integers or characters, use:
a^=b;
b^=a;
a^=b;
When swapping floats or doubles use:
a+=b;
b=a-b;
a=a-b;
When swapping strings use:
a=(char*)((int)a^(int)b);
b=(char*)((int)a^(int)b);
a=(char*)((int)a^(int)b);
This works by modifying the address values of the pointer.
Is This Answer Correct ? | 12 Yes | 2 No |
Answer / shubham
NOne of the answer is correct except the 2ND one.....Please
don't give wrong answers.
Is This Answer Correct ? | 32 Yes | 23 No |
Answer / bhaskar.mantrala
void main()
{
int a,b;
clrscr();
printf("\n Enter the values of a and b ");
scanf(" %d %d ", &a,&b);
a=a*b;
b=a/b;
a=a/b;
printf("\n \n After swapping ----> a = %d \t b = %d",a,b);
getch();
}
Is This Answer Correct ? | 9 Yes | 3 No |
Answer / trinath somarouthu
using X-OR
#define SWAP(x,y) x^=y^=x^=y
x = x ^ y --> x^=y -- (1)
y = y ^ x --> y^=x -- (2)
x = x ^ y --> x^=y -- (3)
(3) in (2) --> y^=x^=y -- (4)
(4) in (1) --> x^=y^=x^=y -- :-)
all togeather, he single line code
#define SWAP(x,y) x^=y^=x^=y
Is This Answer Correct ? | 17 Yes | 13 No |
Answer / sunil kharat
a^=b^=a^=b;
try it...
it works. one line solution
Is This Answer Correct ? | 4 Yes | 0 No |
Answer / perl guru
($a,$b)=($b,$a)
....this is how we do in PERL....Ha Ha Ha pretty cool !!!!
And BELIEVE me it works not only for NUMBERS but also for
STRINGS, ARRAYS or any Data Structure or any garbage
value....this is mine Challenge.....
None of the any programming language can come close to PERL
in this much of SIMPLICITY and ROBUSTNESS....just one line
does the Magic....
Again I would say the Question itself is very silly one "How
to swap two variables, without using third variable
?"....and I see alot of stupid Answers posted here....
Everyone posting the answer is assuming that Question is
about swapping INTEGER NUMBERS....I would like to ask what
if I provide FLOATING POINT NUMBERS, NEGATIVE NUMBERS, REAL
NUMBERS, and surely it does not work for STRINGS....
That is why I say "Perl is immensely powerful. If you think
something can't be done, the problem is likely to be it is
beyond your ability, not that of Perl."
Welcome to the world of PERL....its more precious than PEARL....
Is This Answer Correct ? | 4 Yes | 1 No |
In the following pgm add a stmt in the function fun such that the address of 'a' gets stored in 'j'. main(){ int * j; void fun(int **); fun(&j); } void fun(int **k) { int a =0; /* add a stmt here*/ }
why the range of an unsigned integer is double almost than the signed integer.
write a program to find out roots of quadratic equation "x=-b+-(b^2-4ac0^-1/2/2a"
How will u find whether a linked list has a loop or not?
Write a C program to add two numbers before the main function is called.
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'.
main() { 41printf("%p",main); }8
main() { int a=2,*f1,*f2; f1=f2=&a; *f2+=*f2+=a+=2.5; printf("\n%d %d %d",a,*f1,*f2); }
What is the output for the program given below typedef enum errorType{warning, error, exception,}error; main() { error g1; g1=1; printf("%d",g1); }
#define FALSE -1 #define TRUE 1 #define NULL 0 main() { if(NULL) puts("NULL"); else if(FALSE) puts("TRUE"); else puts("FALSE"); }
main() { int i=_l_abc(10); printf("%d\n",--i); } int _l_abc(int i) { return(i++); }
String reverse with time complexity of n/2 with out using temporary variable.