Swap two Numbers using Third Variable
Here you will learn and get example code of C++ Program to Swap two Numbers using Third Variable.
This is the simple swapping method program using third variable. This program can be done by without using third variable. Here we are using a temp variable, Which is used to change the values of two variables. Let’s understand this simple swapping method by example.
for example:-
a=5 and b=10
temp=a;
a=b;
b=temp;
Program to Swap two Numbers using Third Variable
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #include<iostream> using namespace std; int main() { int x, y, temp; cout<<"\nEnter the value of x and y\n"; cin>>x>>y; cout<<"\nBefore Swapping\nx = "<<x<<"\ny = "<<y; // swapping temp = x; x = y; y = temp; cout<<"\nAfter Swapping\nx = "<<x<<"\ny = "<<y; return 0; } |
Output
Check out our other C++ programming Examples