C Program To Swap Two Numbers Without Using Third Variable

Ex: write a C program to swap two numbers without using third variable. How to swap two numbers without using third variable. C PROGRAM to swap two numbers without using third variable.


In below code we will swap values of two variable without using third variable.
Here, we will use the addition and subtraction for swap two variable's. 



C  Program To  Swap Two Numbers Without Using Third Variable:


#include<stdio.h>
void main()
{

 int a,b;

 printf("Enter the value of a and b\n");
 scanf("%d%d",&a,&b);

 printf("values of a & b before swap\na=%d b=%d",a,b);

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

 printf("\nvalues of a & b after swaping\na=%d b=%d",a,b);

}



Above Program's show's the following output:



C  Program To  Swap Two Numbers Without Using Third Variable




No comments:

Post a Comment

If you have any doubts, please discuss here...👇