Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Swapping two numbers using a temporary variable
2.1.
Pseudo Code
2.2.
Implementation in C++
2.2.1.
Complexity Analysis
3.
Swapping two numbers without using a temporary variable
3.1.
Pseudo Code
3.2.
Implementation in C++
3.2.1.
Complexity Analysis
4.
Swapping two numbers using XOR
4.1.
PseudoCode
4.2.
Implementation in C++
4.2.1.
Complexity Analysis
5.
Frequently Asked Questions
5.1.
What is an XOR operator?
5.2.
What is space complexity?
5.3.
What is time complexity?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Program to swap two numbers

Author Urwashi Priya
2 upvotes
gp-icon
Basics of C++
Free guided path
9 chapters
99+ problems
gp-badge
Earn badges and level up

Introduction

In this blog, we will discuss how to write a program to swap two numbers.

We are given 2 numbers and we need to swap the value.

Example: 

Sample Input:
a=5
b=6

Sample Output:
a=6
b=5

 

We will be discussing the following approaches to swap two numbers

  1. Using temporary variable.
  2. Without using a temporary variable. 
  3. Using XOR operation

Swapping two numbers using a temporary variable

The naive approach to swap two numbers would be to declare a temporary variable. Store the value of the first variable in a temporary variable, move the value of the second variable to the first variable and then move the value of the temporary variable to the second variable.

Time Complexity for writing a program to swap two numbers = O(1).

It takes constant time.

Till now, I assume you must have got the basic idea of what has been asked in the problem statement. So, I strongly recommend you first give it a try.

Program to swap two numbers

Pseudo Code

Algorithm
___________________________________________________________________
procedure swapping():
___________________________________________________________________
1.   Declare a, b, temp
2.   temp=a
3.   a=b
4.   b=temp
5.   Display the value of a and b
end procedure
___________________________________________________________________

Implementation in C++

#include <iostream>
using namespace std;
int main() {
   int a, b, temp;
   cin>>a>>b;
   cout<<"before swapping"<<endl;
   cout<<"a:"<<a<<endl;
   cout<<"b:"<<b<<endl;
   
   //swapping
   temp = a;
   a = b;
   b = temp;
   
   //displaying
   cout<<"after swapping"<<endl;
   cout<<"a:"<<a<<endl;
   cout<<"b:"<<b<<endl;
   
   return 0;
}

 

Output:

Sample Input: 
5
6

Sample Output:
before swapping
a:5
b:6
after swapping
a:6
b:5

 

Complexity Analysis

Time Complexity: O(1), Since no extra operation, constant time is taken.

Space complexity: O(1), no extra variable used. 

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Swapping two numbers without using a temporary variable

To swap two numbers without using a temporary variable, we use the arithmetic operator.

Pseudo Code

Algorithm
___________________________________________________________________
procedure swapping():
___________________________________________________________________
1.   Declare a, b, temp
2.   a=a+b
3.   b=a-b
4.   a=a-b
5.   Display the value of a and b
end procedure
___________________________________________________________________

Implementation in C++

#include <iostream>
using namespace std;
int main() {
   int a, b, temp;
   cin>>a>>b;
   cout<<"before swapping"<<endl;
   cout<<"a:"<<a<<endl;
   cout<<"b:"<<b<<endl;
   
   //swapping
   a = a+b;
   b = a-b;
   a = a-b;
   
   //displaying
   cout<<"after swapping"<<endl;
   cout<<"a:"<<a<<endl;
   cout<<"b:"<<b<<endl;
   
   return 0;
}

 

Output:

Sample Input: 
5
6

Sample Output:
before swapping
a:5
b:6
after swapping
a:6
b:5

 

Complexity Analysis

Time Complexity: O(1), Since no extra operation, constant time is taken.

Space complexity: O(1), no extra variable used. 

Swapping two numbers using XOR

To swap two numbers using XOR, the following concept is kept in mind. The XOR operator always returns the second number if the result of two numbers subjected to XOR is again XOR-ed with the first original number, and returns the first number if the result of two numbers on passing through the XOR operator is again XOR-ed with the second original number.

Example: Say a=5 and b=7
a=5^7=2
b=2^7=5
a=2^5=7.

PseudoCode

___________________________________________________________________
procedure swapping():
___________________________________________________________________
1.   Declare a, b, temp
2.   a=a^b
3.   b=a^b
4.   a=a^b
5.   Display the value of a and b
end procedure
___________________________________________________________________

Implementation in C++

#include <iostream>
using namespace std;
int main() {
   int a, b;
   cin>>a>>b;
   cout<<"before swapping"<<endl;
   cout<<"a:"<<a<<endl;
   cout<<"b:"<<b<<endl;
   
   //swapping
   a = a ^ b;
   b = a ^ b;
   a = a ^ b;
   
   //displaying
   cout<<"after swapping"<<endl;
   cout<<"a:"<<a<<endl;
   cout<<"b:"<<b<<endl;
   return 0;
}

 

Output:

Sample Input: 
5
6


Sample Output:
before swapping
a:5
b:6
after swapping
a:6
b:5

 

Complexity Analysis

Time Complexity: O(1), Since no extra operation, constant time is taken.

Space complexity: O(1), no extra variable used.

Try and compile with online c++ compiler.

You can also read about - Strong number in c

Frequently Asked Questions

What is an XOR operator?

XOR operator is a logical operator returning false if input bits are the same and returns true if input bits are different.

What is space complexity?

Space complexity defines the amount of extra space used in an algorithm.
 

What is time complexity?

Time complexity measures the time taken by a computer to run an algorithm. It is of three types: best time complexity, average time complexity and worst time complexity.

Conclusion

This article taught us how to write a program to swap two numbers.

We discussed its implementation using a temporary variable and without using a temporary variable.

We hope you could take away critical techniques like analysing problems by walking over the execution of the steps and finding out the pattern followed in most array problems.

Also readDecimal to Binary c++

Check out this problem - XOR Queries On Tree

Now, we recommend you practice problem sets based on arrays to master your fundamentals. You can get a wide range of questions similar to this on Coding Ninjas Studio

It's not the end. Must solve the problem of similar types. 

Happy Coding.

Previous article
Program to check whether the number is even or odd
Next article
Program to Find Factorial
Guided path
Free
gridgp-icon
Basics of C++
9 chapters
104+ Problems
gp-badge
Earn badges and level up
Live masterclass