For Swaping Two numbers following procedure is used -
Now we will Explaining above three statements using example ....x = x ^ y --> x^=y -- (1) y = y ^ x --> y^=x -- (2) x = x ^ y --> x^=y -- (3)
Let x = 12 and y = 9 [ For our sake and simplicity consider number is of 4 bits ]
x = 1100 y = 1001
X-OR Table :
A | B | A X-OR B |
1 | 1 | 0 |
1 | 0 | 1 |
0 | 1 | 1 |
0 | 0 | 0 |
Step 1 : After : x = x ^ y
Step 2 : After y = y ^ xx = 1100 y = 1001 ---------- x^y = 0101 ---------- x = 0101 ..... New Value of x
Step 3 : After x = x ^ yx = 0101 ..... New Value is taken y = 1001 ..... Old Value of Y ---------- y^x = 1100 ---------- y = 1100 ..... New Value of y = Initial x
x = 0101 ..... New Value from step 1 y = 1100 ..... New Value of y from Step 2 ---------- y^x = 1001 ---------- x = 1001 ..... New Value of x = Initial y
Here is Program for : [Swap / Interchange two variables [numbers] without using Third Variable]
Output :#include<stdio.h> #include<conio.h> void main() { int num1,num2; printf("\nEnter First Number : "); scanf("%d",&num1); printf("\nEnter Second Number : "); scanf("%d",&num2); num1 = num1 ^ num2; num2 = num1 ^ num2; num1 = num1 ^ num2; printf("\n Numbers after Exchange : num1 = %d and num2 = %d",num1,num2); getch(); }
Enter First Number : 20 Enter Second Number : 40 Numbers after Exchange : num1 = 40 and num2 = 20
0 Comments:
Post a Comment
Your Feedback :This is Growing Site and Your Feedback is important for Us to improve the Site performance & Quality of Content.Feel Free to contact and Please Provide Name & Contact Email