The kids Ram and Anandh are using magic numbers. For example, Ram magic number is 4 and Anandh magic number is 6. The kids exchange their magic number and display the Ram magic number is 6 and Anandh magic number is 4. Implement the above logic using function. [Note: Use call by address]
#include <iostream>
using namespace std;
void magicSwap(int &a, int &b);
int main()
{
int ramMagicNumber = 0;
cout << "Enter Ram magic number: ";
cin >> ramMagicNumber;
int anandhMagicNumber = 0;
cout << "Enter Anandh magic number: ";
cin >> anandhMagicNumber;
cout << "Ram magic number is: " << ramMagicNumber << endl;
cout << "Anandh magic number is: " << anandhMagicNumber << endl;
cout << endl << "Call magicSwap funtion...." << endl << endl;
magicSwap(ramMagicNumber, anandhMagicNumber);
cout << "Now: " << endl;
cout << "Ram magic number is: " << ramMagicNumber << endl;
cout << "Anandh magic number is: " << anandhMagicNumber << endl;
return 0;
}
void magicSwap(int &a, int &b)
{
int temp = 0;
temp = a;
a = b;
b = temp;
}
Comments
Leave a comment