Answer to Question #26378 in C++ for pinak

Question #26378
WAP to swap two numbers without using temporary or third variable?
1
Expert's answer
2013-03-15T10:00:10-0400
#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
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS