Write a function in C++ program to exchange two numbers.
These two variables are passed through parameters of the
function using pointer. void exchange(int *a, int *b)
#include <iostream>
using namespace std;
void exchange(int *a, int *b) {
int tmp = *a;
*a = *b;
*b = tmp;
}
int main() {
int x = 3,y = 4;
cout << "x = " << x << "; y = " << y << endl;
exchange(&x, &y);
cout << "x = " << x << "; y = " << y << endl;
return 0;
}
Comments
Leave a comment