1. Swapping using third variable / temp variable.
Code:
#include<iostream>
#include<conio.h>
using namespace std;
int SwapFunction(int, int);
int main() {
int a, b, result;
cout<<"Enter first Number: ";
cin>>a;
cout<<"Enter second Number: ";
cin>>b;
result = SwapFunction(a, b);
}
int SwapFunction(int x, int y) {
int temp;
temp = x;
x = y;
y = temp;
cout<<"After Swapping First Number : "<<x<<endl;
cout<<"After Swapping Second Number : "<<y<<endl;
}
Output :
Enter first Number: 4
Enter second Number: 3
After Swapping First Number : 3
After Swapping Second Number : 4
2. Swapping without third variable / temp variable.
Code :
#include<iostream>
#include<conio.h>
using namespace std;
int SwapFunction(int, int);
int main() {
int a, b, result;
cout<<"Enter first Number: ";
cin>>a;
cout<<"Enter second Number: ";
cin>>b;
result = SwapFunction(a, b);
}
int SwapFunction(int x, int y) {
x = x + y;
y = x - y;
x = x - y;
cout<<"After Swapping First Number : "<<x<<endl;
cout<<"After Swapping Second Number : "<<y<<endl;
}
Output :
Enter first Number:15
Enter second Number:6
After Swapping First Number :6
After Swapping Second Number :15
IDE Used To Test This Code : DEV C++.
Try this code in your computer for better understanding. Enjoy the code. If you have any Question you can contact us or mail us.We will reply you as soon as possible.
Code:
#include<iostream>
#include<conio.h>
using namespace std;
int SwapFunction(int, int);
int main() {
int a, b, result;
cout<<"Enter first Number: ";
cin>>a;
cout<<"Enter second Number: ";
cin>>b;
result = SwapFunction(a, b);
}
int SwapFunction(int x, int y) {
int temp;
temp = x;
x = y;
y = temp;
cout<<"After Swapping First Number : "<<x<<endl;
cout<<"After Swapping Second Number : "<<y<<endl;
}
Output :
Enter first Number: 4
Enter second Number: 3
After Swapping First Number : 3
After Swapping Second Number : 4
2. Swapping without third variable / temp variable.
Code :
#include<iostream>
#include<conio.h>
using namespace std;
int SwapFunction(int, int);
int main() {
int a, b, result;
cout<<"Enter first Number: ";
cin>>a;
cout<<"Enter second Number: ";
cin>>b;
result = SwapFunction(a, b);
}
int SwapFunction(int x, int y) {
x = x + y;
y = x - y;
x = x - y;
cout<<"After Swapping First Number : "<<x<<endl;
cout<<"After Swapping Second Number : "<<y<<endl;
}
Output :
Enter first Number:15
Enter second Number:6
After Swapping First Number :6
After Swapping Second Number :15
IDE Used To Test This Code : DEV C++.
Try this code in your computer for better understanding. Enjoy the code. If you have any Question you can contact us or mail us.We will reply you as soon as possible.
Post a Comment