Question #26378

WAP to swap two numbers without using temporary or third variable?

Expert's answer

#include <iostream>


int main()
{
//output
std::cout<<"input number A : ";
int a;//create variable
std::cin>>a;//assign user inputed value to a variable
std::cout<<"input number B : ";//output
int b;//create variable
std::cin>>b;//assign user inputed value to a variable

std::cout<<a<<" - "<<b<<std::endl;//outpiut values before swap

//swap
a +=b; //a = a+b
b = a-b;& //
a-=b; //a = a-b

std::cout<<a<<" - "<<b<<std::endl; // output after swap

std::cin.get();//suspend console
std::cin.get();//suspend console
return 0;//exit
}
LATEST TUTORIALS
APPROVED BY CLIENTS