C++ Examples
- Write a C++ Program to print a welcome text in a separate line.
Swap two numbers in C++ using function.
#include <iostream>
using namespace std;
int main()
{
int x,y;
cout<<"Enter the first number to swap ";
cin>>x;
cout<<"Enter the second number to swap ";
cin>>y;
cout<<"The two numbers before swap are "<<x<<" "<<y<<endl;
//inbuilt swap() function - to swap any two numbers
swap(x,y);
cout<<"The two numbers after swap are "<<x<<" "<<y;
return 0;
}
Output
Enter the first number to swap 45
Enter the second number to swap 89
The two numbers before swap are 45 89
The two numbers after swap are 89 45