Wednesday, October 26, 2022

C++ Problem (9) : How to swap two numbers without using third variable?

 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

 How to create a shortcut for a program(application) on windows? Fisrt of all if you are at desktop screen, click right on mouse and click o...