Swap two numbers without using the third variable in c++ ?
#include<iostream.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
//read the two numbers
cout<<endl<<"Enter the value of a and b :";
cin>>a>>b;
//swap the values
cout<<endl<<"Before Swap , a= " <<a<<" b= " <<b;
a=a+b;
b=a-b;
a=a-b;
cout<<endl<<"After Swap , a="<<a<<" b= "<<b;
getch();
}
#include<conio.h>
void main()
{
int a,b;
clrscr();
//read the two numbers
cout<<endl<<"Enter the value of a and b :";
cin>>a>>b;
//swap the values
cout<<endl<<"Before Swap , a= " <<a<<" b= " <<b;
a=a+b;
b=a-b;
a=a-b;
cout<<endl<<"After Swap , a="<<a<<" b= "<<b;
getch();
}
Comments
Post a Comment