Here are C++ programs to swap two numbers, using third variable, without using third variable, using XOR operator, using call by pointers, using call by reference, using #define and using class . Swapping simply means interchanging. For example if x=20 and y=30 than after swapping x=30 and y=20.Now there are various methods to swap two numbers.Some of them are discussed below
C++ programs to swap two numbers using temporary variable
#include<iostream>
using namespace std;
int main()
{
int a=10,b=5,temp;
cout<<"Before swapping a="<<a<<" b="<<b<<endl;
temp=a;
a=b;
b=temp;
cout<<"After swapping a="<<a<<" b="<<b<<endl;
return 0;
}
using namespace std;
int main()
{
int a=10,b=5,temp;
cout<<"Before swapping a="<<a<<" b="<<b<<endl;
temp=a;
a=b;
b=temp;
cout<<"After swapping a="<<a<<" b="<<b<<endl;
return 0;
}
C++ programs to swap two numbers using pointers
#include<iostream>
using namespace std;
int main()
{
int a,b,*p,*q,temp;
cout<<"Enter two numbers : ";
cin>>a>>b;
p=&a;
q=&b;
temp=*p;
*p=*q;
*q=temp;
cout<<"After swapping "<<endl;
cout<<" a1 = "<<a<<endl;
cout<<" a2 = "<<b<<endl;
return 0;
}
using namespace std;
int main()
{
int a,b,*p,*q,temp;
cout<<"Enter two numbers : ";
cin>>a>>b;
p=&a;
q=&b;
temp=*p;
*p=*q;
*q=temp;
cout<<"After swapping "<<endl;
cout<<" a1 = "<<a<<endl;
cout<<" a2 = "<<b<<endl;
return 0;
}
C++ programs to swap two numbers using call by pointers
#include<iostream>
using namespace std;
void swap(int *p,int *q)
{
int temp;
temp=*p;
*p=*q;
*q=temp;
}
int main()
{
int a,b,*p,*q,temp;
cout<<"Enter two numbers : ";
cin>>a>>b;
cout<<"Before swapping "<<endl;
cout<<" a1 = "<<a<<endl;
cout<<" a2 = "<<b<<endl;
swap(&a,&b);
cout<<"After swapping "<<endl;
cout<<" a1 = "<<a<<endl;
cout<<" a2 = "<<b<<endl;
return 0;
}
using namespace std;
void swap(int *p,int *q)
{
int temp;
temp=*p;
*p=*q;
*q=temp;
}
int main()
{
int a,b,*p,*q,temp;
cout<<"Enter two numbers : ";
cin>>a>>b;
cout<<"Before swapping "<<endl;
cout<<" a1 = "<<a<<endl;
cout<<" a2 = "<<b<<endl;
swap(&a,&b);
cout<<"After swapping "<<endl;
cout<<" a1 = "<<a<<endl;
cout<<" a2 = "<<b<<endl;
return 0;
}
C++ programs to swap two numbers using call by reference
#include<iostream>
using namespace std;
void value(int &x,int &y)
{
int temp;
temp=x,
x=y;
y=temp;
}
int main()
{
int x=10,y=20;
cout<<"Before swapping "<<endl;
cout<<"Value of x : "<<x<<endl;
cout<<"Value of y : "<<y<<endl;
value(x,y);
cout<<"After swapping "<<endl;
cout<<"Value of x : "<<x<<endl;
cout<<"Value of y : "<<y<<endl;
return 0;
}
using namespace std;
void value(int &x,int &y)
{
int temp;
temp=x,
x=y;
y=temp;
}
int main()
{
int x=10,y=20;
cout<<"Before swapping "<<endl;
cout<<"Value of x : "<<x<<endl;
cout<<"Value of y : "<<y<<endl;
value(x,y);
cout<<"After swapping "<<endl;
cout<<"Value of x : "<<x<<endl;
cout<<"Value of y : "<<y<<endl;
return 0;
}
C++ programs to swap two numbers without using third variable
#include<iostream>
using namespace std;
int main()
{
int a=10,b=5;
cout<<"Before swapping a="<<a<<" b="<<b<<endl;
b=a+b;
a=b-a;
b=b-a;
cout<<"After swapping a="<<a<<" b="<<b<<endl;
return 0;
}
using namespace std;
int main()
{
int a=10,b=5;
cout<<"Before swapping a="<<a<<" b="<<b<<endl;
b=a+b;
a=b-a;
b=b-a;
cout<<"After swapping a="<<a<<" b="<<b<<endl;
return 0;
}
C++ programs to swap two numbers using bitwise XOR operator
#include<iostream>
using namespace std;
int main()
{
int a=10,b=5;
cout<<"Before swapping a="<<a<<" b="<<b<<endl;
b=a^b;
a=b^a;
b=b^a;
cout<<"After swapping a="<<a<<" b="<<b<<endl;
return 0;
}
using namespace std;
int main()
{
int a=10,b=5;
cout<<"Before swapping a="<<a<<" b="<<b<<endl;
b=a^b;
a=b^a;
b=b^a;
cout<<"After swapping a="<<a<<" b="<<b<<endl;
return 0;
}
C++ programs to swap two numbers without using third variable
#include<iostream>
using namespace std;
int main()
{
int a=10,b=5;
cout<<"Before swapping a="<<a<<" b="<<b<<endl;
b=a*b;
a=b/a;
b=b/a;
cout<<"After swapping a="<<a<<" b="<<b<<endl;
return 0;
}
using namespace std;
int main()
{
int a=10,b=5;
cout<<"Before swapping a="<<a<<" b="<<b<<endl;
b=a*b;
a=b/a;
b=b/a;
cout<<"After swapping a="<<a<<" b="<<b<<endl;
return 0;
}
C++ programs to swap two numbers without using third variable
#include<iostream>
using namespace std;
int main()
{
int a=10,b=5;
cout<<"Before swapping a="<<a<<" b="<<b<<endl;
a=a+b-(b=a);
cout<<"After swapping a="<<a<<" b="<<b<<endl;
return 0;
}
using namespace std;
int main()
{
int a=10,b=5;
cout<<"Before swapping a="<<a<<" b="<<b<<endl;
a=a+b-(b=a);
cout<<"After swapping a="<<a<<" b="<<b<<endl;
return 0;
}
C++ programs to swap two numbers using #define
#include<iostream>
using namespace std;
#define swap(a,b) a^=b^=a^=b;
int main()
{
int a=10,b=5;
cout<<"Before swapping a="<<a<<" b="<<b<<endl;
swap(a,b);
cout<<"After swapping a="<<a<<" b="<<b<<endl;
return 0;
}
using namespace std;
#define swap(a,b) a^=b^=a^=b;
int main()
{
int a=10,b=5;
cout<<"Before swapping a="<<a<<" b="<<b<<endl;
swap(a,b);
cout<<"After swapping a="<<a<<" b="<<b<<endl;
return 0;
}
C++ programs to swap two numbers using class
#include<iostream>
using namespace std;
class swp
{
private:
int a,b,temp;
public:
void fswap(int &a,int &b)
{
temp=a;
a=b;
b=temp;
}
void getdata()
{
cout<<"Enter the value of a : ";
cin>>a;
cout<<"Enter the value of b : ";
cin>>b;
}
void showdata()
{
cout<<"Value of a : "<<a;
cout<<endl<<"Value of b : "<<b;
}
void swap()
{
temp=a;
a=b;
b=temp;
}
};
int main()
{
swp obj;
obj.getdata();
cout<<"Before swapping "<<endl;
obj.showdata();
obj.swap();
cout<<endl<<"After swapping "<<endl;
obj.showdata();
return 0;
}
using namespace std;
class swp
{
private:
int a,b,temp;
public:
void fswap(int &a,int &b)
{
temp=a;
a=b;
b=temp;
}
void getdata()
{
cout<<"Enter the value of a : ";
cin>>a;
cout<<"Enter the value of b : ";
cin>>b;
}
void showdata()
{
cout<<"Value of a : "<<a;
cout<<endl<<"Value of b : "<<b;
}
void swap()
{
temp=a;
a=b;
b=temp;
}
};
int main()
{
swp obj;
obj.getdata();
cout<<"Before swapping "<<endl;
obj.showdata();
obj.swap();
cout<<endl<<"After swapping "<<endl;
obj.showdata();
return 0;
}
No comments:
Post a Comment