Login or Sign Up
Logging in...
Remember me
Log in
Or
Sign Up
Forgot password or user name?
Log in with
Search in titles only
Search in C only
Search
Advanced Search
Forums
Product Launch
Updates
Today's Posts
Member List
Calendar
Home
Forum
Topic
C
interchange two number
Collapse
X
Collapse
Posts
Latest Activity
Photos
Page
of
1
Filter
Time
All Time
Today
Last Week
Last Month
Show
All
Discussions only
Photos only
Videos only
Links only
Polls only
Events only
Filtered by:
Clear All
new posts
Previous
template
Next
anirban
New Member
Join Date:
Sep 2006
Posts:
17
#1
interchange two number
Sep 6 '06, 02:16 AM
is it possibe that interchange two no.
using only two integer in c not in c++
durgaps
New Member
Join Date:
Sep 2006
Posts:
1
#2
Sep 6 '06, 02:20 AM
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
Post
Cancel
anirban
New Member
Join Date:
Sep 2006
Posts:
17
#3
Sep 6 '06, 02:23 AM
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
Post
Cancel
Rakesh Mutharaju
New Member
Join Date:
Sep 2006
Posts:
14
#4
Sep 6 '06, 03:26 AM
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
Post
Cancel
Rakesh Mutharaju
New Member
Join Date:
Sep 2006
Posts:
14
#5
Sep 6 '06, 03:28 AM
to add something more..
a= a*b;
b=a/b;
a=a/b;
will also yeild the same desired output...!!
Comment
Post
Cancel
pukur123
New Member
Join Date:
Sep 2006
Posts:
61
#6
Sep 6 '06, 07:47 AM
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
Post
Cancel
Rakesh Mutharaju
New Member
Join Date:
Sep 2006
Posts:
14
#7
Sep 6 '06, 07:55 AM
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
Post
Cancel
asamum
New Member
Join Date:
Sep 2006
Posts:
1
#8
Sep 6 '06, 01:05 PM
yes it is possible
the deal is swapping
use pointers
or
just
copy
temp=a;
a=b;
b=temp;// temp is temporary variable
Comment
Post
Cancel
anirban
New Member
Join Date:
Sep 2006
Posts:
17
#9
Sep 7 '06, 02:43 AM
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
Post
Cancel
Previous
template
Next
Working...
Yes
No
OK
OK
Cancel
👍
👎
☕
Comment