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

a=10 
b=20

 

Then the result after and before swapping is like that

Before swapping

a=10
b=20

After swapping

a=10
b=20

 

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);
 
   getch();
}

 

 

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/ web developer and professional blogger serving Worldwide on freelancer and ODesk platform to show the capability of his skills He is the founder of pTutorial.com. The main purpose of this site is sharing and exchanging of I.T. Knowledge.

Latest posts by Umar Farooque Khan (see all)