To swap two numbers

This program is to swap two numbers using XOR operator

#include <stdio.h>

int main()
{
  int num1, num2;
  printf("\n Enter the first number: ");
  scanf("%d", &num1);
  printf("\n Enter the second number: ");
  scanf("%d", &num2);
  printf("\n The numbers before swappped are: %d %d", num1, num2);
  num1 = num1 ^ num2;
  num2 = num2 ^ num1;
  num1 = num1 ^ num2;
  
  printf("\n The numbers after swappped are: %d %d", num1, num2);
  return 0;
}
  

Output:


 Enter the first number: 10 
 Enter the second number: 13

 The numbers before swappped are: 10 13
 The numbers after swappped are: 13 10


Categories: C Language

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 )

Connecting to %s

%d bloggers like this: