Site Loader
Get a Quote

How to swap two numbers without using the third variable in c++?

swap two numbers: – Swap any two numbers means the value will be interchanged. It means suppose there are two variable and each has its value and after the swap, their value will be interchanged.

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.

 

Program to swap two numbers in c++

Here we have written a program in c++ to swap any two numbers without using the third variable.we have used header file on the top of the program then used the namespace after that we have taken input from the console end to swap any numbers and used the logic to swap two numbers.

#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;

}

Output:

swap two numbers

Post Author: TechieFlair

Leave a Reply

Your email address will not be published.