Swap values of two variables in C using XOR

Swapping of Two Numbers in C without using XOR: 

First, see the: Swapping of  two numbers in C

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;
}
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:
Share:

Ads

Search This Blog

  • ()
Powered by Blogger.

Strings in C With Syntax, C String Program with output

Strings in C :  Strings can be defined as the one-dimensional array of characters terminated by a null ('\0'). The difference betwee...

Blog Archive