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





30 comments:

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

    would serve

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

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

      Delete
  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

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

    ReplyDelete
    Replies
    1. its using third var...

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

      Delete
  4. THANKS ,THANKS A LOT

    ReplyDelete
  5. good job..

    ReplyDelete
  6. please solve the problem

    int a=~-3;

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

    ReplyDelete
    Replies
    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

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

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

    ReplyDelete
    Replies
    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

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

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

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

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

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

      Delete
  11. u r most welcome

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

    ReplyDelete
  13. Process 1 & 5 are approx same.

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

    ReplyDelete
    Replies
    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..

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

    www.programmerschat.blogspot.in

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

    ReplyDelete
  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

    ReplyDelete
  18. This comment has been removed by the author.

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

    ReplyDelete