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;
}





35 comments:

TRINATH SOMAROUTHU said...

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

would serve

Priyanka kumari said...

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

Unknown said...

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();
}

Anonymous said...

THANKS ,THANKS A LOT

Anonymous said...

good job..

Jhon said...

please solve the problem

int a=~-3;

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

Unknown said...

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

Anonymous said...

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

Anonymous said...

its using third var...

Anubhav said...

Here is couple of more way to swap two numbers without using temp variable in Java

Anonymous said...

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

Anonymous said...

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

Anonymous said...

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

Anonymous said...

u r most welcome

Anonymous said...

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

Anonymous said...

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

Unknown said...

HI can u please help me to solve the following
Swap the values in 2 variBles using a third variAble?? Plzzzzz rply its urgent

Unknown said...

Process 1 & 5 are approx same.

Anonymous said...

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

Girish Patel said...

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

Ashok Natarajan said...

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

Swaping done.

Anonymous said...

@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..

Anonymous said...

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

Abc said...

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

www.programmerschat.blogspot.in

Anonymous said...

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

Unknown said...

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);
}

Unknown said...

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();
}

Unknown said...
This comment has been removed by the author.
Unknown said...

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

inderjeet said...

if do not use third variable then

Unknown said...

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

Shayed Mollah said...

plz help me
1 + X +( X2 / !2 )+ ( X3 / !3 ) + ( X4 / !4 )

Shayed Mollah said...

plz help me
swap two numbers without using third variable

subham kumar said...

Nice article . There is good coding example collection visit
Top coding program example

Suresh Dasari said...

Good questions on c programming language. Will share it on our programming site.