C++ Program To Swap Two Numbers Without Using Third Variable
All Answers
need an explanation for this answer? contact us directly to get an explanation for this answer
Multiply/Divide
#include<iostream>
using namespace std;
void swpa(int ,int );
int main()
{
int a,b;
cout<<"Enter Two Number You Want To Swap :\n";
cin>>a>>b;
cout<<"\nAfter Swapping Numbers Are Given below\n\n";
swap(a,b);
cout<<a<<"\t"<<b<<" \n";
return 0;
}
void swap(int x,int y)
{
//without using third variable
x=x*y;
y=x/y;
x=x/y;
}
Output:
Enter Two Number You Want To Swap :
1000 2000
After Swapping Numbers Are Given below
2000 1000
need an explanation for this answer? contact us directly to get an explanation for this answer
need an explanation for this answer? contact us directly to get an explanation for this answer
Plus/Minus
#include<iostream>
using namespace std;
void swpa(int ,int );
int main()
{
int a,b;
cout<<"Enter Two Number You Want To Swap :\n";
cin>>a>>b;
cout<<"\nAfter Swapping Numbers Are Given below\n\n";
swap(a,b);
cout<<a<<"\t"<<b<<" \n";
return 0;
}
void swap(int x,int y)
{
//without using third variable
x=x+y;
y=x-y;
x=x-y;
}
Output:
Enter Two Number You Want To Swap :
100 200
After Swapping Numbers Are Given below
200 100
need an explanation for this answer? contact us directly to get an explanation for this answertotal answers (3)
Bitwise Operator
Output:
Enter Two Number You Want To Swap :
10000
20000
After Swapping Numbers Are Given below
20000 10000
need an explanation for this answer? contact us directly to get an explanation for this answer