C Program to Swap Two Variables Without Using Third Variable

In this program, you will learn how to swap two variables without using third variable. There are five different types of swapping are here-


Example-1

#include<stdio.h>
int main(){
int a=5,b=10;
a=b+a;
b=a-b;
a=a-b;
printf("a= %d b= %d",a,b);
getch();
}



swap two variable
swap two variables without using third variable





Example-2

#include<stdio.h>
int main(){
int a=5,b=10;
a=5;
b=10;
a=a+b-(b=a);
printf("\na= %d b= %d",a,b);
getch();
}




swap two variable
swap two variables without using third variable




Example-3

#include<stdio.h>
int main(){
int a=5,b=10;
a=5;
b=10;
a=a^b;
b=a^b;
a=b^a;
printf("\na= %d b= %d",a,b);
getch();
}



swap two variable
swap two variables without using third variable






Example-4

#include<stdio.h>
int main(){
int a=5,b=10;
a=5;
b=10;
a=b-~a-1;
b=a+~b+1;
a=a+~b+1;
printf("\na= %d b= %d",a,b);
getch();
}



swap two variable
swap two variables without using third variable




Example-5

#include<stdio.h>
int main(){
int a=5,b=10;
a=5,
b=10;
a=b+a,b=a-b,a=a-b;
printf("\na= %d b= %d",a,b);
getch();
}



swap two variable
swap two variables without using third variable

Post a Comment

0 Comments