C program to swap two number without third extra variable.

Posted by Mangesh on April 11, 2018

/* program to swap two variable without extra variable */

Description :

We need to exchange the value of variable. i.e if there are two variable x, y and x = 5, y= 6 then our program should make x=6, y=5.

Logic :

Here we are not using third variable, instead we are swaping two number by using mathematical operation.

Program :

void main()
  int number1,number2;
  printf("Enter the first number : ");
  printf("Enter the second number : ");
  printf("\nFirst number = %d, second number = %d.",number1,number2);
  number1 = number1 + number2;
  number2 = number1 - number2;
  number1 = number1 - number2;
  printf("\nAfter Swaping!!!!!");
  printf("\nFirst number = %d, second number = %d.",number1,number2);

Output :

C program for swap without third variable Executed and Tested in Turbo C 3.2

Written with from Mangesh.

Related Post
1 C program to get date.
2 C program for Hello World.
3 C program to swap two number without third extra variable.
4 C program to find square root of a number.
5 C program for printing random number.
Latest Post
1 shubhanshu
2 test
3 C program for Breadth First Search.
4 C program for Depth First Search(DFS).
5 C for circular queue using linked list.