473,246 Members | 1,349 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,246 software developers and data experts.

interchange two number

17
is it possibe that interchange two no.
using only two integer in c not in c++
Sep 6 '06 #1
8 3422
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.
Sep 6 '06 #2
anirban
17
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
Sep 6 '06 #3
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.!! :)
Sep 6 '06 #4
to add something more..

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

will also yeild the same desired output...!!
Sep 6 '06 #5
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
Sep 6 '06 #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

yes you are very much right.. i was just considering the case of the particular input which he has mentioned.
Sep 6 '06 #7
asamum
1
yes it is possible

the deal is swapping

use pointers
or
just
copy
temp=a;
a=b;
b=temp;// temp is temporary variable
Sep 6 '06 #8
anirban
17
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
Sep 7 '06 #9

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

Similar topics

8
by: EAS | last post by:
Hey, I'm new to python (and programming in general) so I'll prolly be around here a lot... Anyways, I've found out how to make a "guess my number game" where the player guesses a number between...
4
by: Sreejith K | last post by:
How to interchange the two nibbles of a byte without using any bitwise operator.
11
by: don | last post by:
Ok, this is a homework assignment, but can you help me out anyway...... I need a routine for figuring out if a number inputted by the user is a prime number or not...... all I'm asking for is Not...
3
by: Bernhard Hidding | last post by:
Hello, my c++ programm produces a variable number of ASCII files with variable file names. These are to be plotted in with gnuplot. I use system("gnuplot -persist my_gnuplot_script.gpl"); to...
2
by: Hank Reed | last post by:
I found only five small threads discussing Access and EDI and none of them were very recent. Is anyone interfacing with EDI using Access? Can anyone recommend a book that would explain the basics...
0
by: WideBoy | last post by:
Hi All, I was wondering if any one can point me in the direction of some web resource that fulfills the information I am looking for. Basically, I need some hard evidence of how XML schema and...
2
by: Sam | last post by:
Hello all, Can we interchange the rows & columns of a Datagrid with VB.NET so that all the data that is displayed in rows is displayed as columns? ThanX.
3
momotaro
by: momotaro | last post by:
am trying to impliment the Interchange trees is this function is correct? int Interchange(Tree *root, Tree **newroot){ Tree *temps; if (root){ temp =...
0
by: =?Utf-8?B?S3VydA==?= | last post by:
I was wondering if anyone knew if there was a simple way to convert a Data Interchange Format (.dif) file (see:http://en.wikipedia.org/wiki/Data_Interchange_Format) into a dataset using the native...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.