Swap two variable Without using third variable in C by R4R Team

What is Swapping ?

-In programming language, swap between two variable means that we exchange the value of two variable.

Example-
Before Swapping
a=1
b=2
After swapping
a=2
b=1

program-

#include < stdio.h>
int main()
{
int a,b,temp;
printf("Enter First Number\n");
scanf("%d",&a);
printf("Enter Second Number\n");
scanf("%d",&b);
printf("Before Swapping\n");
printf("a=%d,b=%d",a,b);
//Swap logic
a=a+b;
b=a-b;
a=a-b;
printf("\nAfter Swapping\n");
printf("a=%d,b=%d",a,b);
}


output-

Enter First Number
23
Enter Second Number
56
Before Swapping
a=23,b=56
After Swapping
a=56,b=23

-In this program, we take an input of the two number
-then swap these variable using third variable and logic is a=a+b,b=a-b,a=a-b then print both value to see the change.




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!