how to swap two numbers without using a third variable

How to swap two numbers without using a third variable ?

C program to swap two numbers without using third variable, swapping means changing the data from one variable to other variable see the following example-

You have two variable a and b with value



Then the result after and before swapping is like that

Before swapping


After swapping



The below c program swap or c example the numbers without using third variable. You can swap the number by using third variable also. See the example in next section.

C program to swapp numbers

#include <stdio.h>
#include <conio.h>
void main()
   int firstnumber, secondnumber, temp;
   printf("Enter the value of firstnumber and secondnumber\n");
   scanf("%d%d", &firstnumber, &secondnumber);
   printf("Before Swapping\nfirstnumber = %d\nsecondnumber = %d\n",firstnumber,secondnumber);
   temp = firstnumber;
   firstnumber    = secondnumber;
   secondnumber    = temp;
   printf("After Swapping\nfirstnumber = %d\nsecondnumber = %d\n",firstnumber,secondnumber);



For more c example, c tutorial orc tutorial and example PDF visit C Example

Do not forgot to like share and comment

The following two tabs change content below.
Umar Farooque Khan is Software developer and professional blogger serving Worldwide on freelancer and ODesk platform to show the capability of his skills. He is the founder of The main purpose of this site is sharing and exchanging of Knowledge related to the recent technologies.