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

Codetextpro Provides free Web tutorial, HTML, CSS, C, C++, Java, JavaScript, PHP, MySql, Python, Program, Programming Notes, Programming Questions and more.

Programming

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