**Swap two numbers without using a third variable | Swap Two numbers without using a temporary variable: **There are 4 tricks to **swap two numbers without using a third variable**. You can use of them for your program. However the trick #4 is the smartest, fastest & easiest of all. The trick used is nothing but basic mathematics fundamentals like: multiplication, division and addition , subtraction. Further explanation is given below:

### **Trick #1**

**Trick #1**

**Trick #1: Addition, subtraction**A=A+B;

B=A-B;

A=A-B

*Program #1:* **Swap two numbers without using third variable using addition and subtraction**.

#include <stdio.h> int main() { int a,b; printf("Enter two numbers to swap (a & b) : "); scanf("%d",&a); scanf("%d",&b); printf( "Values before swapping:\nValue of a =%d \nValue of b=%d",a,b); a=a+b; b=a-b; a=a-b; printf( "\nValues after swapping:\nValue of a =%d \nValue of b=%d",a,b); }

### **Trick #2**

**Trick #2**

**Trick #2: Multiplication, Division**A=A X B;

B=A/B;

A=A/B;

*Program #2:* **Swap two numbers without using third variable** using Multiplication and division

#include <stdio.h> int main() { int a,b; printf("Enter two numbers to swap (a & b) : "); scanf("%d",&a); scanf("%d",&b); printf( "Values before swapping:\nValue of a =%d \nValue of b=%d",a,b); a=a*b; b=a/b; a=a/b; printf( "\nValues after swapping:\nValue of a =%d \nValue of b=%d",a,b); }

### **Trick #3**

**Trick #3**

*Trick #3: XOR Operation,*A=A^B;

B=A^B;

A=A^B;

*Program #3: ***Swap two numbers without using third variable*** : XOR operation trick.*

#include<stdio.h> int main() { int a,b; printf("Enter two numbers to swap (a & b) : "); scanf("%d",&a); scanf("%d",&b); printf( "Values before swapping:\nValue of a =%d \nValue of b=%d",a,b); a=a^b; b=a^b; a=a^b; printf( "\nValues after swapping:\nValue of a =%d \nValue of b=%d",a,b); }

### **Trick #4**

**Trick #4**

**Trick #4:**

*Basic logic: 1 Line Trick**,**A=A+B – (B=A);*

Program #4: **Swap two numbers without using third variable**: Basic Logic; 1 Line trick.

#include<stdio.h> int main() { int a,b; printf("Enter two numbers to swap (a & b) : "); scanf("%d",&a); scanf("%d",&b); printf( "Values before swapping:\nValue of a =%d \nValue of b=%d",a,b); a=a+b-(b=a); printf( "\nValues after swapping:\nValue of a =%d \nValue of b=%d",a,b); }