Using - C
#include<stdio.h>
void Swap(int *a, int *b);
void main() {
int a, b;
printf("Enter the value of a: ");
scanf("%d",&a);
printf("Enter the value of b: ");
scanf("%d",&b);
system("cls");
printf("Before Swapping: a = %d & b = %d",a,b);
Swap(&a,&b);
printf("\nAfter Swapping : a = %d & b = %d",a,b);
}
void Swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
OUTPUT
Enter the value of a: 9
Enter the value of b: 3
-------------------------------------------------
Before Swapping: a = 9 & b = 3
After Swapping : a = 3 & b = 9
Using - C++
#include<iostream>
using namespace std;
class Swapper {
public:
void Swap(int *a, int *b);
};
void Swapper::Swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int a, b;
Swapper object;
cout<< "Enter the value of a: ";
cin>> a;
cout<< "Enter the value of b: ";
cin>> b;
system("cls");
cout<< "Before Swapping: a = "<< a<< " & b = "<< b<< endl;
object.Swap(&a,&b);
cout<< "After Swapping : a = "<< a<< " & b = "<< b<< endl;
return 0;
}
OUTPUT
Enter the value of a: 10
Enter the value of b: 17
-------------------------------------------------
Before Swapping: a = 10 & b = 17
After Swapping : a = 17 & b = 10