Skip to content
doubtly

1 answer

Best answer

#include <iostream>
using namespace std;

void swap(int& x, int& y) {
 int temp = x;
 x = y;
 y = temp;
}

int main() {
 int a = 10;
 int b = 20;

 cout << "Before swapping: a = " << a << ", b = " << b << endl;

 swap(a, b);

 cout << "After swapping: a = " << a << ", b = " << b << endl;

 return 0;
}

In this program, we define a function swap() that takes two integer references x and y as input, and swaps their values using a temporary variable temp.

In the main() function, we define two integers a and b with initial values of 10 and 20, respectively. We then call the swap() function with a and b as arguments, which swaps their values.

Finally, we print the values of a and b before and after swapping to verify that the swapping operation worked correctly.

I hope this helps!


Program can be found here :


https://onlinegdb.com/ynfLO1363

image

Doubtly · 26 Mar 2023 Report
Was this answer helpful?

Your answer

Log in or create a free account to answer this question.