Swapping of Two number program in C using XOR:
#include<stdio.h>
int main()
{
int a;
int b;
printf("Type value of A = ");
scanf("%d",&a);
printf("\nType value of B = ");
scanf("%d",&b);
a ^= b;
b ^= a;
a ^= b;
printf("A = %d",a);
printf("\nB : %d",b);
return 0;
}
int main()
{
int a;
int b;
printf("Type value of A = ");
scanf("%d",&a);
printf("\nType value of B = ");
scanf("%d",&b);
a ^= b;
b ^= a;
a ^= b;
printf("A = %d",a);
printf("\nB : %d",b);
return 0;
}
Output:
Type value of A = 7
Type value of B = 5
A = 5
B = 7
Explanation:
- Two integers a, b is declared of Type int.
- The values of a, b are taken from the keyboard given by the user.
- Now both the values of "a" and "b" are changed using Xor.
- And last Printf Prints the value of “a” and “b”.
See also: