Home » NO IDEA » Swapping Two Number without using temporary Variable

Swapping Two Number without using temporary Variable

Blog Stats

  • 15,880 hits
January 2013
M T W T F S S
« Dec   Mar »
 123456
78910111213
14151617181920
21222324252627
28293031  

Swapping two number means interchanging the value stored in the two variables this can be done in various ways one is

using the temp variables which follows

temp=variable1

variable1=variables2

variables2=temp

when we print the variable1 and variable two we have the swapped values

In this below program we have swapped value using XOR relation

example

a=5(0101)

b=6(0110)

a=a^b

a=0101^0110 //a=0011

b=a^b

b=0011^0110 //b=0101

a=a^b

a=0101^0110 //a=0110

check out this site for calculation

http://www.miniwebtool.com/bitwise-calculator/

//sawpping of two number using temp number
#include<stdio.h>
int main()
{
int num1,num2;
printf("\n\t\t Swapping two number ");
printf("\n Enter the two number ");
scanf("%d%d",&num1,&num2);
printf("\n The number before swapping %d %d",num1,num2);
num1=num1^num2;
num2=num1^num2;
num1=num1^num2;
printf("\n The number After swapping %d %d",num1,num2);
return 0;
}
Advertisements

1 Comment

  1. […] Swapping Two Number without using temporary Variable […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: