Example & Tutorial understanding programming in easy ways.

Write a program to swap two variable without use third variable.

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.




Read More →