How to swap the content oftwo variables without a temporary variable

Questions by udaykalai   answers by udaykalai

Showing Answers 1 - 27 of 27 Answers

sivasubrmaniam

  • Mar 29th, 2006
 

x = x + yy = x - yx = x - y

Sharath

  • Mar 31st, 2006
 

void swap(int a,int b)

{

a =a+b;

b=a-b;

a=a-b;

}

amit kumar

  • Mar 31st, 2006
 

x=x+y

x=(x+y)-x;

y=(x+y)-x;

  Was this answer useful?  Yes

sanesh raj

  • Mar 31st, 2006
 

x = x + yy = x - yx = x - y

  Was this answer useful?  Yes

Geeta.Sanikop

  • Apr 3rd, 2006
 

Hello!!It can be done by calling a Function with the concept of Pointers.Also its done using ^ operator.Just try it!!

  Was this answer useful?  Yes

meeran

  • Apr 12th, 2006
 

# include#includemain(){void swap(int*,int*);int a,b;puts("enter the number to swap");scanf("%d",&a);scanf("%d",&b); swap(&a,&b);printf("%d,%d",a,b);getch();}void swap(int*a,int*b){*a=*a+*b;*b=*a-*b;*a=*a-*b;return;}

  Was this answer useful?  Yes

Yada Kishore

  • Apr 12th, 2006
 

Swapping two ints x,y using XORx = x ^ y;y = x ^ y;x = x ^ y;

  Was this answer useful?  Yes

ganapathy

  • Apr 16th, 2006
 

hi,

#include<stdio.h>

main()

{

int a,b;

b-=(a=(b+=a)-a);

}

ZZ

  • Apr 23rd, 2006
 

Cannot use y=x+y x=y-x y=y-x

because it may be beyond the rang of the variable.

always uses XOR version

  Was this answer useful?  Yes

smithi

  • May 6th, 2006
 

the most easy logic for swap is:

a=(a+b)-(b=a);

  Was this answer useful?  Yes

lappy

  • May 9th, 2006
 

this will fail if either x or y is 0

  Was this answer useful?  Yes

Mahendar

  • May 22nd, 2006
 

hello everyone....nice to see the solutions from u guys............i have one question for u all, hw du u swap two variables (having string contents) without making use of any third variable ???? try to find it out ...!!!

  Was this answer useful?  Yes

Chieh Haan

  • May 26th, 2006
 

how to usw a Function with the concept of Pointers to deal with this problem?I think it will confront with the problem of overflowed.

  Was this answer useful?  Yes

ARUNESHWAR

  • Jul 8th, 2006
 

we can swap in the following ways:

1. b=a+b-(a=b);

2.b=a+b;a=b-a;b=b-a;

3.b=a^b;a=a^b;b=a^b;

  Was this answer useful?  Yes

gpreddy

  • Nov 8th, 2006
 

This won't work if any one of two nymbers is 0

  Was this answer useful?  Yes

Using XOR, we can swap two numbers. we use pointers as we can only return one number, to return two numbers, we must pass by reference. The following code is in C.

void XOR_SWAP (int *a, int *b)
{
if (a != b)
{

*a ^= *b;
*b ^= *a;
*a ^= *b;
}
}

  Was this answer useful?  Yes

paragmalshe

  • May 29th, 2008
 

if(a>=0&&b>=0||a<0&&b;<0)
{
    if(a>=b)
    {
     a=a-b;
     b=a+b;
     a=b-a;
    }
    else
   {
    b=b-a;
    a=a+b;
    b=a-b;
   }
}
else
{
a=a+b;
b=a-b;
a=a-b;
}

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions