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 |
Write a routine to implement the polymarker function
Write a program to model an exploding firecracker in the xy plane using a particle system
write a program in c language to get the value of arroy keys pressed and display the message which arrow key is pressed?
Given only putchar (no sprintf, itoa, etc.) write a routine putlong that prints out an unsigned long in decimal.
6 Answers Fusion Systems GmbH,
main() { int i=0; while(+(+i--)!=0) i-=i++; printf("%d",i); }
9 Answers CSC, GoDB Tech, IBM,
Write a C program that defines a 2-dimentional integer array called A [50][50]. Then the elements of this array should randomly be initialized either to 1 or 0. The program should then print out all the elements in the diagonal (i.e. a[0][0], a[1][1],a[2][2], a[3][3], ……..a[49][49]). Finally, print out how many zeros and ones in the diagonal.
int swap(int *a,int *b) { *a=*a+*b;*b=*a-*b;*a=*a-*b; } main() { int x=10,y=20; swap(&x,&y); printf("x= %d y = %d\n",x,y); }
main() { int i=4,j=7; j = j || i++ && printf("YOU CAN"); printf("%d %d", i, j); }
A program that will create a movie seat reservation. The program will display the summary seats and its status. The user will be ask what seat no. to be reserved, then it will go back again to the summary to display the updated seat status. If the seat no. is already reserved then it will prompt an error message. And also if the input seat no is out of range then it will also prompt an error message. The program is continuously running. Termination of the program will depends on how the programmer will apply. Sample output: Movie Seats Reservation Summary of Seats: Seat 1: Available Seat 2: Available Seat 3: Available Seat 4: Available Seat 5: Available Enter seat no. (Press 0 to terminate Or the assigned seat capacity) : 1 Movie Seats Reservation Summary of Seats: Seat 1: Reserve Seat 2: Available Seat 3: Available Seat 4: Available Seat 5: Available Enter seat no. (Press 0 to terminate Or the assigned seat capacity) : 6 The Seat no. is out of range! Movie Seats Reservation Summary of Seats: Seat 1: Reserve Seat 2: Available Seat 3: Available Seat 4: Available Seat 5: Available Enter seat no. (Press 0 to terminate Or the assigned seat capacity) : 1 The Seat no. is already reserved! Movie Seats Reservation Summary of Seats: Seat 1: Reserve Seat 2: Available Seat 3: Available Seat 4: Available Seat 5: Available Enter seat no. (Press 0 to terminate Or the assigned seat capacity) : 0 GoodBye... Thank You!!!
Who could write how to find a prime number in dynamic array?
void main() { void *v; int integer=2; int *i=&integer; v=i; printf("%d",(int*)*v); }
#include <stdio.h> main() { char * str = "hello"; char * ptr = str; char least = 127; while (*ptr++) least = (*ptr<least ) ?*ptr :least; printf("%d",least); }