C Program to swap two numbers without using third variable


In this program, You will learn how to swap two numbers without using third variable in C.


Before swap: 10 20

After swap: 20 10


Example: How to swap two numbers without using third variable in C

Run it Now
#include<stdio.h>

int main() {

   int a, b;

   printf("Enter Two Numbers :");
   scanf("%d%d", &a, &b);

   a = a + b;
   b = a - b;
   a = a - b;

   printf("\nAfter Swapping value of a is :%d", a);
   printf("\nAfter Swapping value of b is :%d", b);

   return 0;
}

Output:

Enter Two Numbers :16 17
After Swapping value of a is :17
After Swapping value of b is :16