Q1: Write a C++ code to swap two integer numbers and find the sum of them (use temp variable).
#include <iostream>
using namespace std;
int main()
{
int a, b, temp;
cout<<"enter the value of a ";
cin >> a;
cout<<"enter the value of b ";
cin >> b;
cout<< " before a = "<<a<<", b = "<<b<<endl;
temp = a;
a = b;
b=temp;
int sum = a + b;
cout<<" after a = "<<a<<", b = "<<b<<endl;
cout<<" the sum is : "<<sum<<endl;
return 0;
}
👍1
Q2: Write a C++ code to swap two integer numbers (use Bitwise exclusive OR operator).
#include <iostream>
using namespace std;
int main()
{
int a, b, temp;
cout<<"enter the value of a ";
cin >> a;
cout<<"enter the value of b ";
cin >> b;
cout<< " before a = "<<a<<", b = "<<b<<endl;
a = a ^ b;
b = a ^ b;
a = a ^ b;
cout<<" after a = "<<a<<", b = "<<b<<endl;
return 0;
}
❤1👍1