A step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
This tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.
Creating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.
How to swap two numbers without using a third variable |
On Sunday, October 7th 2007 at 03:04 PM By Andrew Pociu (View Profile) ![]() ![]() ![]() ![]() (Rated 4.2 with 13 votes) |
||
|
Using pointers you can easily swap two variables without using an additional variable to store a temporary value. This is because instead of swapping values, you can swap addresses, as shown in this function: void SwapSmart(int * a, int * b) |
|||
Digg It!
Del.icio.us
Reddit
StumbleIt
Newsvine
Furl
BlinkList
|
|||
|
|||
Current CommentsYou may do something like this
void swap(int *a, int *b){
*a=*a+*b;
*b=*a-*b;
*a=*a-*b;
}
Really Good
That would be what is known as the infamous Butterfly Switch.
ISO-C++ provides a swap function, so you don\'t need to write one yourself at all.
It is possible two swap 2 numbers without using arithmetic and bitwise operators?
If so how. Pl. send the code.
It is possible two swap 2 numbers without using arithmetic and bitwise operators?
If so how. Pl. send the code.
query..
how to swap to 2 numbers in one single text box using php program
its very simple
a=a+b;
b=a=b;
a=a-b
its very simple
a=a+b;
b=a-b;
a=a-b;
a = a ^ b
b= a ^ b
a = a^ b
thanks ruchita................!
Guys u're fantastic..........
Guys u're fantastic..........
Thanks dude! u r fantastic
you all are dangerous
Is this program suitable for values a=65534 and b=65533
Thank u
How about for character???
How it will be looks like in C#?
a=a*b;
b=a/b;
a=a/b;
hi purna ,ur answer is not giving right value when the value of variable is 0.
thik u........sol my problem.
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleFunSwap
{
class Program
{
static void swap(int x,int y)
{
int c;
c = x;
x = y;
y = c;
}
static void Main(string[] args)
{
int a, b;
a = 300;
b = 200;
swap(a, b);
System.Console.WriteLine("{0}{1}", a, b);
System.Console.ReadLine();
}
}
}
"Here in values of a,b are swapped in function,
but when it come's to main again it's retaining its original values and i want the swapped values to be printed on console". if any one can help me out please.
how to swap two numbers using arithmetic operator.i want whole program.
A=10
B=5
A=A B;
A=10 5
A=15
B=A-B;
B=15-5
B=10;
A=A-b;
A=15-10;
A=5
A=A B;
B=A-B;
A=A-B;
Hey! Can anyone tell me how one would swap strings in C/C without using a third variable and using pointers? i tried using memcpy() the results were not very satisfying.
@srikanth
This should work, you need to use "ref".
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleFunSwap
{
class Program
{
static void swap(ref int x, ref int y)
{
int c;
c = x;
x = y;
y = c;
}
static void Main(string[] args)
{
int a, b;
a = 300;
b = 200;
swap(ref a, ref b);
System.Console.WriteLine("{0}{1}", a, b);
System.Console.ReadLine();
}
}
}
thanx a lot.........
thanx a lot.........
Hai .... plz tell me how to write a c program to swap 3 values using "class"..
How to write a c program using class to swap 3 values ..?
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y,temp;
clrscr();
void swap(int,int)
printf("enter the two numbers");
scanf("%d%d",
thank u ...........
thank u ...........
thank u ...........
thank u ...........
thank u ...........
I LOVE C PROGRAMMING.
PLS.SEND ME A SOFT COPY THROUGH MY EMAIL.
THANKS.
I LOVE C PROGRAMMING.
PLS.SEND ME A SOFT COPY OF C TEXT BOOK THROUGH MY EMAIL.
THANKS.
I LOVE C PROGRAMMING.
PLS.SEND ME A SOFT COPY OF C TEXT BOOK THROUGH MY EMAIL.
THANKS.
thanks that u like c programming..
by the what r u doing roght now???
i mean r u a student or any professional???
plz give ur complete email address and i will try for giving the soft copy to u.......
thanks for commenting..........
with regards daisy
thanks that u like c programming..
by the what r u doing roght now???
i mean r u a student or any professional???
plz give ur complete email address and i will try for giving the soft copy to u.......
thanks for commenting..........
with regards daisy
this is another method to swap two numbers without using a variable::
#include<stdio.>
#include<conio.h>
void main()
{
int x,y;
clrscr();
printf("enter any two numbers");
scanf("%d%d",
thanks for the info(ruchi)
#include<iostream.h> #include<conio.h>
void swap(int
can we swap two numbers without use of any third variable and using only bitwise and(
can we swap two numbers without use of any third variable and using only bitwise and(
can we swap two numbers without use of any third variable and using only bitwise and(
A=1 , B=0
One Way
B=A OR B
A=A AND B
another way
A= A AND B
B= NOT(A)
there might many more
to abbas
i dont think ur program is write when i take
A=2 B=3
i am not able to swap the numbers using bitwise and and bitwise or operator as said by you in your code
to abbas
i dont think ur program is write when i take
A=2 B=3
i am not able to swap the numbers using bitwise and and bitwise or operator as said by you in your code
for both the cases after swaping i am getting result as 3,3 when i have taken A=2,B=3
and for A=1,B=0 i am getting the result after swapping as 0,0
oh Sandy Now Check
A=1 , B=0
One Way
B=A OR B
A=A NAND B
another way
A= A AND B
B= NOT(A)
there might many more
to abbas
no this isn't working too please check the code you have posted. next time run it on any compiler and do upload if u get any successful swapping technique...
to abbas
no this isn't working too please check the code you have posted. next time run it on any compiler and do upload if u get any successful swapping technique...
Sandy:
Assume two numbers a and b.
the logic here to be used is
a=a b;
b=a-b;
a=a-b;
eg: a=5,b=6
a=5 6 ie.a=11
b=11-6 ie.b=5
a=11-5 ie a=6
a=6 and b=5 thus they are swapped ma... take care all the best
Sandy
Dear that Swaping for BIT using AND Operator
Please send me shorting programs in c
I have to need it.
Hey shank
I am asking a pgm for swapping of two variables with out using third variable and also using bitwise and/or operators only. Not, using any other arithmetic operators
dats really very useful.. thanks a lot..
dats really very useful.. thanks a lot..
dats really very useful.. thanks a lot..
dats really very useful.. thanks a lot..
//Swapping of 2 variables without using 3rd variable
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("Enter a = ");
scanf("%d",
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
printf("enter two numbers");
scanf("%d%d",
prasanna ur program is very superb thanks for sent mail,do u hav any other pgm pls mail me
sandy,
i'll send it by tomo
sandy,
i'll send it by tomo
sandy,
i'll send it by tomo
sandy,
i'll send it by tomo
sandy,
i'll send it by tomo
sandy,
i'll send it by tomo
sandy,
i'll send it by tomo
sandy,
i'll send it by tomo
sandy,
i'll send it by tomo
sandy,
i'll send it by tomo
sandy,
i'll send it by tomo
sandy,
i'll send it by tomo
A =B; B=A-B; A=B-A
is not working for high values, because they will go out of range!
A*=B; B=A/B; A=A/B
same problem as above and also is not working if one of the values is 0
A^=B; B^=A; A^=B (^= meaning xor ;)
can't see any problems with this!
you can get xor with and,or and not
e.g.: A xor B = (A not and B) and (A or B)
but normally the system already gives you xor, when you got and/or/not :)
a=((a b)-(b=(a-b) b))
a=((a b)-(b=(a-b) b))
Related Knowledge Base Articles
Related Source Code
Related Tutorials
C++ Job Search