Program using third variable.
#include<iostream> using namespace std; int main() { int a=10 ,b=50; //initialize variable with values; int temp; //third temporary variable is declared. cout<<"a ="<<a<<" " <<"b ="<<b<<endl; //value before swaping temp = a; a = b; b = a; cout<<"a ="<<a<<" " <<"b ="<<b<<endl; //value before swaping system("pause"); }
Program without using third variable.
#include<iostream> using namespace std; int main() { int a=10 ,b=50; //initialize variable with values; cout<<"a ="<<a<<" " <<"b ="<<b<<endl; //value before swaping a=a+b; b=a-b; a=a-b; cout<<"a ="<<a<<" " <<"b ="<<b<<endl; //value before swaping system("pause"); }
0 comments:
Post a Comment