Swap Two Numbers

Swap Two Numbers” is a basic programming exercise for beginners. Here, we are given two numbers, entered by user and our task is to write a program to swap the numbers.

Example:

Before Swapping: 
A = 10, B = 20
After Swapping
A = 20, B = 10

There are basically two ways to swap two number. These are:

  1. With using extra variable
  2. Without using extra variable

The time complexity of both the solutions is O(1).

METHOD 1: With extra variable

To swap two numbers by using extra variable, we will copy the first number to extra variable. Then, copy the second number to first variable. Lastly, copy the extra variable number to second number.

C++ Program to swap two number with using extra variable is as follows:

#include<bits/stdc++.h>  
using namespace std;  
int main()  
{  
    int num1,num2;  
      
    //Scan the number  
    cout<<"Enter the numbers:";  
    cin>>num1>>num2;  
      
    //Numbers Before swapping  
cout<<"Numbers Before Swapping”<<endl;  
  
    cout<<"NUM1: "<<num1<<endl;  
    cout<<"NUM2: "<<num2<<endl;  
      
    int temp;  
    // copy the first number to temp variable  
    temp = num1;  
    // copy the second number to first number  
    num1 = num2;  
    // copy the temp variable to first number  
    num2 = temp;  
      
    //Numbers After swapping  
    cout<<"Numbers After Swapping”<<endl;  
    cout<<"NUM1: "<<num1<<endl;  
    cout<<"NUM2: "<<num2<<endl;  
} 
OUTPUT:
Enter the numbers: 21 31
Numbers Before Swapping
NUM1: 21
NUM2: 31
Numbers After Swapping
NUM1: 31
NUM2: 21

METHOD 2: Without extra variable

Arithmetic or bitwise XOR operators can be to swap two number without using third variable.

  1. Using Arithmetic Operator

C++ Program to swap two numbers using arithmetic operator is as follows:

#include<bits/stdc++.h>  
using namespace std;  
int main()  
{  
    int num1,num2;  
      
    //Scan the number  
    cout<<"Enter the numbers:";  
    cin>>num1>>num2;  
      
    //Numbers Before swapping  
    cout<<"Numbers Before Swapping\n";  
    cout<<"NUM1: "<<num1<<endl;  
    cout<<"NUM2: "<<num2<<endl;  
      
    num1 = num1 + num2;  
    num2 = num1 - num2;  
    num1 = num1 - num2;  
      
    //Numbers After swapping  
    cout<<"Numbers After Swapping\n";  
    cout<<"NUM1: "<<num1<<endl;  
    cout<<"NUM2: "<<num2<<endl;  
}  
OUTPUT:
Enter the numbers: 21 31
Numbers Before Swapping
NUM1: 21
NUM2: 31
Numbers After Swapping
NUM1: 31
NUM2: 21

C++ Program to swap two numbers using Bitwise XOR operator is as follows:

#include<bits/stdc++.h>  
using namespace std;  
int main()  
{  
    int num1,num2;  
      
    //Scan the number  
    cout<<"Enter the numbers:";  
    cin>>num1>>num2;  
      
    //Numbers Before swapping  
    cout<<"Numbers Before Swapping\n";  
    cout<<"NUM1: "<<num1<<endl;  
    cout<<"NUM2: "<<num2<<endl;  
      
    num1 = num1 ^ num2;  
    num2 = num1 ^ num2;  
    num1 = num1 ^ num2;  
      
    //Numbers After swapping  
    cout<<"Numbers After Swapping\n";  
    cout<<"NUM1: "<<num1<<endl;  
    cout<<"NUM2: "<<num2<<endl;  
}  

OUTPUT:
Enter the numbers: 21 31
Numbers Before Swapping
NUM1: 21
NUM2: 31
Numbers After Swapping
NUM1: 31
NUM2: 21

Related Posts:

  1. Program to print Fibonacci Series up to Nth Term.
  2. Program to find and print nth Fibonacci number.
  3. Program to find Power of the Number.
  4. Program to find Quotient and Remainder.
  5. Program to find largest amongst three numbers.
  6. Program to find factorial of a number.
  7. Program to find GCD of two numbers.
  8. Program to find LCM of two numbers.
  9. Program to check whether entered number is odd or even.
  10. Program to check whether entered number is prime number or not.
  11. Program to check whether entered number is palindrome or not.
  12. Program to check whether entered number is Armstrong Number or Not.
  13. Program to convert binary number to octal number.
  14. Program to convert binary number to decimal number.
  15. Program to convert binary number to hexadecimal number.
  16. Program to convert octal number to binary number.
  17. Program to convert octal number to decimal number.
  18. Program to convert octal number to hexadecimal number.
  19. Program to convert decimal number to binary number.
  20. Program to convert decimal number to octal number.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *