C Program to Swap Two Variables Without Using Third Variable - CodeTextPro | Web Tutorial | Technology

CodeTextPro.com Provides - Web design, Technology gyan, Digital marketing, Online earnings, Seo, Adsense, Technology news, Motivation and inspiration, Latest Technology Affairs, Gadgets, Blogging

Thursday, July 4, 2019

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

No comments:

Post a Comment