Computer Science, asked by theshaikh123, 8 months ago

Q.Write a program in C++ that exchanges data (passing by reference) using swap function to interchange the given two nos.

Answers

Answered by sanjiththesupernigha
0

Answer:

Using temporary variables :

#include <iostream>

using namespace std;

int main()

{

   int a = 5, b = 10, temp;

   cout << "Before swapping." << endl;

   cout << "a = " << a << ", b = " << b << endl;

   temp = a;

   a = b;

   b = temp;

   cout << "\nAfter swapping." << endl;

   cout << "a = " << a << ", b = " << b << endl;

   return 0;

}

Without using temporary variables :

#include <iostream>

using namespace std;

int main()

{

   

   int a = 5, b = 10;

   cout << "Before swapping." << endl;

   cout << "a = " << a << ", b = " << b << endl;

   a = a + b;

   b = a - b;

   a = a - b;

   cout << "\nAfter swapping." << endl;

   cout << "a = " << a << ", b = " << b << endl;

   return 0;

}

hope it helps

Explanation:

Similar questions