swap two numbers without using third variable
Answers
Answered by
1
#include<iostream.h>
#include<conio.h>
{clrscr();
int a,b;
cout<<"enter 2 numbers";
cin>>a>>b;
a=a+b;
b=a-b;
a=a-b;
cout<<"/n after swapping value of a="<<a;
cout<<"/n after swapping value of b="<<b;
getch();
}
#include<conio.h>
{clrscr();
int a,b;
cout<<"enter 2 numbers";
cin>>a>>b;
a=a+b;
b=a-b;
a=a-b;
cout<<"/n after swapping value of a="<<a;
cout<<"/n after swapping value of b="<<b;
getch();
}
Answered by
3
Answer:
#include<iostream>
using namespace std;
int main()
{
int x, y, t;
cin>>x>>y;
cout<<"Before swapping"<<" "<<"a= "<<x<<" " <<"and"<<" "<<"b="<<y<<"\n";
t = x;
x = y;
y = t;
cout<<"After swapping"<<" "<<"a= "<<x<<" "<<"and"<<" "<<"b="<<y;
return 0;
}
Explanation:
Similar questions