C Program to swap two numbers in a single line


In this program, You will learn how to swap two numbers in a single line in c.


Before swap: 10 20

After swap: 20 10

Example: How to swap two numbers in a single line in c.

#include<stdio.h>

int main() {

  int x, y;

  printf("Enter two numbers:");
  scanf("%d%d", &x,&y);

  x ^= y ^= x ^= y;

  printf("After swap x is :%d", x);
  printf("\nAfter swap y is :%d", y);

  return 0;
}

Output:

Enter two numbers:12 13                                                                                                                
After swap x is :13                                                                                                                    
After swap y is :12