swap two numbers

Swap two numbers using C++ Program?

Swap two numbers mean that the value will be interchanged. It means suppose there are two variables and contain respective value and after compile the program their respective value change to each other.

swap two number example: – A whose value is 10 and B whose value is 20 when you perform the swapping the value of A will assign to B and value of B will assign to A now the value of A will become 20 and the value of B will become 10.

C++ program to swap two numbers in C++

#include<iostream>

#include<conio.h>

using namespace std;

int main()

{

            int a,b;

            cout<<"\nEnter two numbers : ";

            cin>>a>>b;

            a=a+b;

            b=a-b;

            a=a-b;

            cout<<"\nAfter swapping numbers are : ";

            cout<<a<<" "<<b;

           getch();

            return 0;

            }

swap two number Output :

swap two numbers
swap two numbers

 

In the above example, it will take the two value for a and b from the user suppose the user has taken 12 for a and 24 for b after performing the swapping it will become 24 12 as shown in above example.

I hope you will like this session.Good Luck!

Leave a Comment