C++ (9) : How to swap two numbers without using third variable?
#include<iostream>
using namespace std;
int main(){
double a,b;
cout<<"*****Swapping two numbers without using third variable.*****"<<endl;
cout<<"Enter first number."<<endl;
cin>>a;
cout<<"Enter second number."<<endl;
cin>>b;
cout<<"Before swapping:"<<endl;
cout<<"a = "<<a<<endl;
cout<<"b = "<<b<<endl;
a=a+b;
b=a-b;
a=a-b;
cout<<"After swapping:"<<endl;
cout<<"a = "<<a<<endl;
cout<<"b = "<<b<<endl;
return 0;
}
Output:
*****Swapping two numbers without using third variable.*****
Enter first number.
10
Enter second number.
5
Before swapping:
a = 10
b = 5
After swapping:
a = 5
b = 10
No comments:
Post a Comment