## INDEX

### SWAP TWO VARIABLES WITHOUT USING THIRD USING C PROGRAM VARIABLE

Swapping in c without temporary variable

Swap 2 numbers without using third variable in c

How to swap two numbers in c without using third variable

#include<stdio.h>

int main(){

int a=5,b=10;

//process one
a=b+a;
b=a-b;
a=a-b;
printf("a= %d  b=  %d",a,b);

//process two
a=5;b=10;
a=a+b-(b=a);
printf("\na= %d  b=  %d",a,b);

//process three
a=5;b=10;
a=a^b;
b=a^b;
a=b^a;
printf("\na= %d  b=  %d",a,b);

//process four
a=5;b=10;
a=b-~a-1;
b=a+~b+1;
a=a+~b+1;
printf("\na= %d  b=  %d",a,b);

//process five
a=5,b=10;
a=b+a,b=a-b,a=a-b;
printf("\na= %d  b=  %d",a,b);

return 0;
}

1. #define SWAP(x,y) x^=y^=x^=y

would serve

1. Hi trinath your this program do my help & made my image in class is a intelligenc boy so thenku very much

2. one more way
#include
#include

int main( )

{
static int a=2,b=5;

a=a*b;

b=a/b;
a=a/b;

printf(" a =%d\t b=%d",a,b);
}

2. hi TRINATH SOMAROUTHU
Thank you for posting another solution
#define swap(x,y) x^=y^=x^=y
void main()
{
int a=10,b=20;
swap(a,b);
clrscr();
printf("%d %d" ,a,b);
getch();
}
output: 20 10

3. void main()
{
int a,b,c;
clrscr();
printf("\n Enter two no ");
scanf("%d%d",&a,&b);

printf("\n Before Swaping value of a and b %d %d",a,b);

c=a;
a=b;
b=c;

printf("\n After Swaping value of a and b %d %d",a,b);

getch();
}

1. its using third var...

2. read the question first. we don't have to use third variable.

3. if do not use third variable then

4. THANKS ,THANKS A LOT

5. good job..

int a=~-3;

actually i do,not understand about ~ operator that how exactly it works .please solve step by step

1. First take 2's complement of -3 which will be as:
3
= 0000 0011 // binary value of 3
= 1111 1100 // take 1's complement i.e invert 0's & 1's
2's complement = 1111 1101 // add 1

We have got result for -3. Now operation for ~ (1's complement is pending). We know in 1's complement 0's will replaced by 1's & 1's will be replaced by 0's as we did above in second step.

Therefore 1's complement of 1111 1101 will be =
0000 0010
Result = 2

7. process 4 is not working
error is 'wrong type argument to bit-complement' in float value

8. can anybody explain in detail how a=a+b-(b=a); will work out for swapping

1. a=a+b-(b=a)
now since value of a will be assigned to b
so equation will be
a=a+b-(a)
a=b

9. learn c programming and c graphics from programmingcampus
where you can get best online tutorials which make simple and easy to your learning.

10. a=a+b
a=a-b
b=a-b

1. thank u very much. this is the best solution without using third variable. simple and short

2. the right order is a=a+b,b=a-b,a=a-b

3. yupp...its correct order..(y)

11. u r most welcome

Swap the values in 2 variBles using a third variAble?? Plzzzzz rply its urgent

13. Process 1 & 5 are approx same.

14. Ashok Natarajan9/20/13, 8:27 PM

I have got another simple solution for this.
A=A*B
B=A/B
A=A/B

Swaping done.

1. @Ashok Natarajan this is also correct when given both values some numbers.. if your give A=0,B=4 that time floating exception will happen..

15. Swapping of two numbers can be done in 5 methods u can see in following url...

www.programmerschat.blogspot.in

16. void main()
{
int a,b;
clrscr();
printf("\n Enter two no :");
scanf("%d%d",&a,&b);

printf("\n Before Swaping value of a and b %d %d",a,b);

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

printf("\n After Swaping value of a and b %d %d",a,b);

getch();
}

17. Very Nice Article helps a lot ...
You can also find 6 type of logic to swap two number here check one it will be helpful ...
Swapping of two numbers

18. This comment has been removed by the author.

19. process 4 is not working. I things it's
b= b - ~a - 1;
a= a + ~b +1;
what u say?

20. How to swap 2 variable using only assignment operator and no other operator in C

21. Nice Examples... visit java examples with solution java examples