Do you know to swap using 3rd variable ?
If yes then learn who to swap without using 3rd variable
#include<iostream>
using namespace std;
void main()
{
//Swapping without 3rd variable
int a=10,b=20; //Value of a=10 and b=20
cout<<"Value of 'a' before Swapping: "<<a<<endl;
cout<<"Value of 'b' before Swapping: "<<b<<endl;
a=a+b; //now a=a(10)+b(20) -> a=30
b=a-b; //now b=a(30)-b(20) -> b=10
a=a-b; //now a=a(30)-b(10) -> a=20
cout<<"Value of 'a' after swapping: "<<a<<endl;
cout<<"Value of 'b' after swapping: "<<b<<endl;
system("pause");

No comments:
Post a Comment