1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

swaping two variables without third variable.

Discussion in 'C' started by imported_crmahato, Mar 12, 2010.

  1. imported_crmahato

    imported_crmahato New Member

    Joined:
    Mar 9, 2010
    Messages:
    23
    Likes Received:
    0
    Trophy Points:
    0
    First Method

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


    Second Method

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


    Third Method

    a=a^b;
    b=a^b;
    a=a^b;
     
  2. pankaj.sea

    pankaj.sea New Member

    Joined:
    Apr 6, 2009
    Messages:
    461
    Likes Received:
    13
    Trophy Points:
    0
    Occupation:
    Web Developer
    Location:
    Kolkata
    Home Page:
    nice post too! this is too useful!
     
  3. creative

    creative New Member

    Joined:
    Feb 15, 2010
    Messages:
    87
    Likes Received:
    0
    Trophy Points:
    0
    another thing i would like to add in this post i.e, Swappin using Pointer!
    just declare a function before void main() and follow the codes!

    Code:
    void swap(int*,int*);
    void main()
    {
    [i]Program Statement[/i]
    }
    
    void swap(int *i,int *j)
    int k;
    k=*i;
    *i=*j;
    *j=k;
    printf("%d %d",*i,*j);
    }
     
  4. pankaj.sea

    pankaj.sea New Member

    Joined:
    Apr 6, 2009
    Messages:
    461
    Likes Received:
    13
    Trophy Points:
    0
    Occupation:
    Web Developer
    Location:
    Kolkata
    Home Page:
    :)
    yes this is another smart way for swapping!
     
  5. creative

    creative New Member

    Joined:
    Feb 15, 2010
    Messages:
    87
    Likes Received:
    0
    Trophy Points:
    0
    Thanks Pankaj!
    ;)
     

Share This Page