473,396 Members | 1,773 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Swaping Values

Ali Rizwan
925 512MB
I have two variables suppose a an b of integer type.
Now i want to swap their values.
Suppose if a =10 and b=5 then a=5 and b=10
But not want to declare another variable like

temp=a
a=b
b=temp

i don't want to use this style.

I have done a lot of things but sometime it only swap when first value is bigger than 2nd and only for when both values are evene or values are even odd if i input both odd nothing done.

and i have not the code yet have done it in my univ.
So somebody can help me
Thanx
Dec 12 '07 #1
5 1624
pp08
3
Hi

To Swap to value (let a =10 & b =5 to a = 5 & b = 10) u can follow following process without using 3rd variable as u have done (temp)

a = a+b;

b = a-b;

a = a-b;

Hope this will help some..

Best Regards,
PP
Dec 12 '07 #2
femina
35
you could also use bitwise xor ^ operator in swapping two numbers without an intermediate variable
Expand|Select|Wrap|Line Numbers
  1.     a      b    output
  2.     0      0       0
  3.     0      1       1
  4.     1      0       1
  5.     1      1       0
Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3. int a=10,b=20;
  4. a=a^b;
  5. b=b^a;
  6. a=a^b;
  7. printf("%d   %d",a,b);
  8. }
Dec 12 '07 #3
Ali Rizwan
925 512MB
Hi

To Swap to value (let a =10 & b =5 to a = 5 & b = 10) u can follow following process without using 3rd variable as u have done (temp)

a = a+b;

b = a-b;

a = a-b;

Hope this will help some..

Best Regards,
PP
Thanx for the reply
But i think this will only work for those in which one value will half of other suppoes
4 and 8
3 and 6
but not for
3 and 8
But ill try it hope this will worj=k

Thanx
>> Ali <<
Dec 13 '07 #4
pp08
3
Hi Ali,

Let me explain the swapping code according to ur values i.e. a=3 & b =8

a = a + b (in this case a value = 11, correct but B is same as usual so b =8)

b = a - b (as a= 11 and b =8, so a-b =3 is now the value of b)

a = a-b (as a= 11 and b =3 , so a-b =8 is now the value of a)

so atlast we got a = 8 and b = 3

Hope this will give a clear picture..

Have A nice time
Thanks
pp
Dec 14 '07 #5
This is case is also works for ur question.

a=a*b;
b=a/b;
a=a/b
Dec 14 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Steve Hall | last post by:
Folks, My secnario involves two tables - ObservationRegister, and Person. ObservationRegister contains most of the "useful" fields, including the UserID of the person that raised the record, and...
6
by: cipher | last post by:
I have some constant values in my web service that my client application will require. Having to keep server side and client side definitions insync is tedious. I am trying to do something like...
2
by: Hennie | last post by:
I apologise if this is a stupid question, but I would appreciated any help on this subject. I want to create a view (VIEW_1 in example below) where I take numeric values from a field in one...
8
by: aleksandar.ristovski | last post by:
Hello all, I have been thinking about a possible extension to C/C++ syntax. The current syntax allows declaring a function that returns a value: int foo(); however, if I were to return...
5
by: biju242 | last post by:
In my project i have a datagrid the values that are shown are taken from the database additional values can also be entere through a textbox on that form. i would like to add a dropdown list to that...
11
by: mukeshrasm | last post by:
Hi I want to swap the content of rows up or down based on the corresponding button clicked. <table border="0" width="200"> <tr> <td>first name </td><td>last name</td><td...
41
by: Andry | last post by:
Hi, Is there any way in C++ swap two values without using temp("third variable").if yes thne how we can ?
8
by: gigonomics | last post by:
Hi all, I hope someone can help me out. I need to return the best available seats subject to the constraint that the seats are side by side (or return X consecutive records from a table column...
2
by: montakin | last post by:
I want to know details about swaping in c or c++ language and give me some example code on it....please..
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.