interchange two number

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • anirban
    New Member
    • Sep 2006
    • 17

    interchange two number

    is it possibe that interchange two no.
    using only two integer in c not in c++
  • durgaps
    New Member
    • Sep 2006
    • 1

    #2
    Originally posted by anirban
    is it possibe that interchange two no.
    using only two integer in c not in c++
    Yes, definitely.
    Use XOR property, ie

    p = p XOR b
    changes p according to value of b

    p = p XOR b
    you get back the original p.

    Comment

    • anirban
      New Member
      • Sep 2006
      • 17

      #3
      my problem is following
      i give input of two no.

      a=5
      b=7
      both are integer

      my required output will be
      a = 7
      b= 5

      without using third variable

      Comment

      • Rakesh Mutharaju
        New Member
        • Sep 2006
        • 14

        #4
        int a, b;
        a= 5;
        b= 7;

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

        this will give the requied solution.. there are couple of more solutions to this..
        the XOR suggested above is also correct.!! :)

        Comment

        • Rakesh Mutharaju
          New Member
          • Sep 2006
          • 14

          #5
          to add something more..

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

          will also yeild the same desired output...!!

          Comment

          • pukur123
            New Member
            • Sep 2006
            • 61

            #6
            Always use XOR operations.

            Let us assume that both a and b are integers and suppose maximum value that an integer can store be 1000.

            let a=500 and b=700 then the methods suggested above will fail to give the right answers.

            so the correct way is to use the XOR operations

            Comment

            • Rakesh Mutharaju
              New Member
              • Sep 2006
              • 14

              #7
              Originally posted by pukur123
              Always use XOR operations.

              Let us assume that both a and b are integers and suppose maximum value that an integer can store be 1000.

              let a=500 and b=700 then the methods suggested above will fail to give the right answers.

              so the correct way is to use the XOR operations

              yes you are very much right.. i was just considering the case of the particular input which he has mentioned.

              Comment

              • asamum
                New Member
                • Sep 2006
                • 1

                #8
                yes it is possible

                the deal is swapping

                use pointers
                or
                just
                copy
                temp=a;
                a=b;
                b=temp;// temp is temporary variable

                Comment

                • anirban
                  New Member
                  • Sep 2006
                  • 17

                  #9
                  Originally posted by Rakesh Mutharaju
                  int a, b;
                  a= 5;
                  b= 7;

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

                  this will give the requied solution.. there are couple of more solutions to this..
                  the XOR suggested above is also correct.!! :)

                  thanks your solution help me to solve the problem

                  Comment

                  Working...