Write a program to swap two values so that what is in Value1 would be interchanged with what is in value 2

Recommended Answers

All 2 Replies

#include <iostream>

using namespace std;
void swapNumbers(int &x, int &y)
{
    int tmp= x;
    x=y;
    y=tmp;

}

int main()
{
    int a,b;
    cin >> a;
    cin >> b;
    cout<< "Numbers are " <<a <<" "<< b <<endl;
    swapNumbers(a,b);
    cout << "Swaped numbers are " << a <<" "<< b << endl;
}

C++ bult-in function

#include <iostream>

using namespace std;

int main()
{
    int a,b;
    cin >> a;
    cin >> b;
    cout<< "Numbers are " << a <<" "<< b <<endl;
    swap(a,b);
    cout << "Swaped numbers are " << a <<" "<< b << endl;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.