A Program to Swap two Numbers
In this example, we are giving a program to swap two numbers which are given by the user.
Program to swap two numbers:
#include<iostream>
using namespace std;
int main() {
double first, second, temp;
cout << "Enter first number: ";
cin >> first;
cout << "Enter second number: ";
cin >> second;
// Value of first number assigned into temp
temp = first;
// Value of second is assigned to first
first = second;
// Value of temp is assigned to second
second = temp;
cout << "After swapping, first number = " << first << endl;
cout << "After swapping, second number = " << second << endl;
return 0;
}
Output:
Enter first number: 53.3 Enter second number: 42.2 After swapping, first number = 42.2 After swapping, second number = 53.3