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 / marutikutre

x = y | (y=x)-x;

&
a=a*b/(b=a); provided either of a or b is not 0

Is This Answer Correct ?    1 Yes 0 No

How to swap two variables, without using third variable ?..

Answer / adi

answer no. 3 wont work in case of b=0!!!

Is This Answer Correct ?    1 Yes 0 No

How to swap two variables, without using third variable ?..

Answer / mrx

In assembly language you could use "xchg" instruction witch
swaps the two variables in one instruction.

/* swaps %dest <-> %source */
xchg %dest, %source


/* inline assambly example for C language */
void swap(int register *x, register int *y)
{
asm("xchg %0, %1;" \
: "=r"(*x), "=r"(*y)
: "0"(*x), "1"(*y)
);
}

This is the fastest method you can achieve in hand
optimization. I think C compiler will do the same trick if
possible.

Is This Answer Correct ?    1 Yes 0 No

How to swap two variables, without using third variable ?..

Answer / simhachalam lakkakula

Answer 4 get fail for the following values

Before: a= 219455078, b= -747788332
After: a= -875186568, b= 1

Is This Answer Correct ?    1 Yes 0 No

How to swap two variables, without using third variable ?..

Answer / simhachalam lakkakula

Answer 4
Logic:
a=a*b;
b=a/b;
a=a/b;

get fail for the following values

Before: a= 219455078, b= -747788332
After: a= -875186568, b= 1

Is This Answer Correct ?    1 Yes 0 No

How to swap two variables, without using third variable ?..

Answer / suresh bhandari

---------------------
a=a*b
b=a/b
a=a/b
---------------------
a=a+b
b=a-b
a=a-b
----------------------
a = a Xor b
b= a Xor b
a= a Xor b
----------------------
all above three methods are correct, i hv checked...

Is This Answer Correct ?    2 Yes 1 No

How to swap two variables, without using third variable ?..

Answer / d

a=a-b
b=b+a
a=b-a


is this wrong?

Is This Answer Correct ?    14 Yes 14 No

How to swap two variables, without using third variable ?..

Answer / rahul

I am new in C/C++.
How to write the program for second answer. Does it
automatically convert the numbers in binary by using ^ sign.

:}

Is This Answer Correct ?    0 Yes 0 No

How to swap two variables, without using third variable ?..

Answer / devarajan.k

a[12]=name
b[20]=name
Here the size 12 & 25 has no effect hence we can change
the size
a[]=name
b[]=name

Is This Answer Correct ?    0 Yes 0 No

How to swap two variables, without using third variable ?..

Answer / xyz

Congrats to all guys who have tried this.Everything u post
here is correct

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More C Code Interview Questions

Cluster head selection in Wireless Sensor Network using C programming language.

0 Answers  


How can I Create a C program in splitting set of characters to specific subsets. Example: INPUT SET OF CHARACTERS: Therefore, my dear brothers and sisters, stand firm. Let nothing move you. Always give yourselves fully to the work of the Lord, because you know that your labor in the Lord is not in vain. SPLIT INTO HOW MANY CHARACTERS PER SUBSETS: 10 OUTPUT: Therefore, my dear b rothers an d sisters, stand fir m. Let not hing move you. Alway s give you rselves fu lly to the work of t he Lord, b ecause you know that your labo r in the L ord is not in vain.

0 Answers  


main() { char *a = "Hello "; char *b = "World"; clrscr(); printf("%s", strcat(a,b)); } a. Hello b. Hello World c. HelloWorld d. None of the above

3 Answers   HCL,


Given an array of characters which form a sentence of words, give an efficient algorithm to reverse the order of the words (not characters) in it.

2 Answers   Wipro,


Ramesh’s basic salary is input through the keyboard. His dearness allowance is 40% of basic salary, and house rent allowance is 20% of basic salary. Write a program to calculate his gross salary.

1 Answers  






Question: We would like to design and implement a programming solution to the reader-writer problem using semaphores in C language under UNIX. We assume that we have three readers and two writers processes that would run concurrently. A writer is to update (write) into one memory location (let’s say a variable of type integer named temp initialized to 0). In the other hand, a reader is to read the content of temp and display its content on the screen in a formatted output. One writer can access the shared data exclusively without the presence of other writer or any reader, whereas, a reader may access the shared memory for reading with the presence of other readers (but not writers).

1 Answers  


main() { char *p; printf("%d %d ",sizeof(*p),sizeof(p)); }

6 Answers  


void main() { int *i = 0x400; // i points to the address 400 *i = 0; // set the value of memory location pointed by i; }

2 Answers  


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

2 Answers  


how to delete an element in an array

2 Answers   IBM,


main() { int i=10; i=!i>14; Printf ("i=%d",i); }

1 Answers  


main() { int *ptr=(int*)malloc(sizeof(int)); *ptr=4; printf("%d",(*ptr)+++*ptr++); }

3 Answers   GNITC,


Categories